# Blogs On Programming

by Vibrant Publishers on May 22, 2022
by Vibrant Publishers on May 22, 2022
by Vibrant Publishers on May 22, 2022
by Vibrant Publishers on May 22, 2022
by Vibrant Publishers on May 22, 2022
The concept of data compression is very common in computer applications. Information is transmitted as a bit-stream of 0’s and 1’s over the network. The goal is to transmit information over the network with a minimum number of bits. Such transmission is faster in terms of speed and bandwidth. Huffman Coding is one such technique of data compression. This blog discusses Huffman coding, how it works, its implementation, and some applications of the method.   What is Huffman Coding? This data compression method assigns codes to unique pieces of data for transmission. The data byte occurring most often is assigned a shorter code whereas the next most occurring byte is assigned a bit longer code. In this way, the whole data is encoded into bytes. The two types of encoding methods used by Huffman coding are Static Huffman encoding and Dynamic Huffman encoding. Static Huffman encoding encodes text using fixed-sized codes. Dynamic Huffman coding, on the other hand, encodes according to the frequency of data characters. This approach is often known as variable encoding.   Huffman Coding Implementation: Huffman coding is done in two steps. The first step is to build a binary tree of data characters depending upon the character frequency. The second step is to encode these binary codes in bits of 0’s and 1’s.   Building a Huffman Tree: The binary tree consists of two types of nodes; leaf nodes and parent nodes. Each node contains the number of occurrences of a character. The parent node or internal node has two children. The left child is indicated by bit 0 and right child by bit 1. The algorithm for building a Huffman binary tree using a priority queue includes the following steps: 1- Each character is added to the queue as a leaf node. 2- While the queue is not empty, pick two elements from the queue front, and create a parent node with these two as child nodes. The frequency of the parent node is the sum of these two nodes. Add this node to the priority queue. Consider the following example for better understanding. Suppose we have characters p, q, r, s, and t with frequencies 4, 6, 7, 7, and 16. Now, build the binary tree:       Encoding the Huffman binary tree: As discussed above, the left nodes are assigned 0 bit and right nodes 1 bit. In this way, codes are generated for all paths from the root node to any child node.   Huffman Compression technique: While building the Huffman tree, we have applied the technique of Huffman compression. It is also called Huffman encoding. We have encoded the data characters into the bits of 0s and 1s. This method reduces the overall bit size of the data. Hence, it is called the compression technique. Let’s see how our above data characters p, q, r, s, and t are compressed.     Considering each character is represented by 8 bits or 1 byte in computer language, the total bit size of data characters (4 p, 6 q, 7 r, and 16 t) is 40 * 8 = 320. After the Huffman compression algorithm, the bit size of data reduces to 40 + 40 + 88 = 168 bits.   Huffman Decompression Technique: For the decompression technique, we need codes. Using these codes, we traverse the Huffman tree and decode the data. We start from the root node, assign 0 to the left node, and 1 to the right node. When a leaf node encounters, we stop.     To decode character t, we will start from the root node, and traverse the path of 111 till we reach a leaf node that is, t.   Time Complexity of Huffman Coding Algorithm: As Huffman coding is implemented using a binary tree, it takes O(nlogn) time, where n is the number of data characters compressed.   Ending Note: In this blog on Huffman coding, we have discussed an algorithm that forms the basis of many compression techniques used in software development. Various compression formats like GZIP and WinZip use Huffman coding. Image compression techniques like JPEG and PNG also work on the Huffman algorithm. Although it is sometimes deemed as a slow technique, especially for digital media compression, the algorithm is still widely used due to its storage efficiency and straightforward implementation.   Get one step closer to your dream job!   Prepare for your programming interview with Data Structures & Algorithms Interview Questions You’ll Most Likely Be Asked. This book has a whopping 200 technical interview questions on lists, queues, stacks, and algorithms and 77 behavioral interview questions crafted by industry experts and interviewers from various interview panels.
by Vibrant Publishers on May 22, 2022
by Vibrant Publishers on May 22, 2022
by Vibrant Publishers on May 22, 2022
by Vibrant Publishers on May 22, 2022
by Vibrant Publishers on May 22, 2022
by Vibrant Publishers on May 21, 2022
Introduction: One of the most basic requirements of a programming language is to perform mathematical operations. Java is an Object-Oriented Programming language. It provides a rich set of operators to manipulate variables. In this blog, let’s take a look at the different types of Java operators and a few examples of sample code.   We can classify the Java operators into the following groups: Arithmetic Operators Relational Operators Bit Operators Logical Operators Assignment Operator       Arithmetic Operators: Arithmetic operators are used for mathematical expressions. In the following table, you will find a list of all arithmetic operators, as well as their descriptions and example usage.   The examples in the table given below assume that the value of the integer variable X is 20 and the value of variable Y is 40.         One thing to note here is the number of operands involved in each case. While addition, subtraction, multiplication & Java modulo operators require two operands, the increment & decrement operators operate on a single operand.   The sample code given below will show all the use-cases of the above-mentioned operators in detail.     public class TestArithmaticOperators {   public static void main(String [] args){     //operand variable declaration and initializatoin.     int a = 5; int b = 22; int c = 16; int d = 33;     System.out.printIn(“a + b =” + (a + b));     System.out.printIn(“a – b =” + (a – b));     System.out.printIn(“a * b =” + (a * b));     System.out.printIn(“b / a =” + (b / a));     System.out.printIn(“b % a =” + (b % a));     System.out.printIn(“c % a =” + (c % a));     System.out.printIn(“a++ =” + (a++));     System.out.printIn(“a– =” + (a–));       // Check the difference between d ++ and ++ d     System.out.printIn(“d++ =” + (d++));     System.out.printIn(“++d =” + (++d));   } }   The following output is thus produced:     \$ javac TestArithmeticOperators.java \$java -Xmx128M -Xms16M TestArithmaticOpertors a + b = 27 a – b = -17 a * b = 110 b / a = 4 b % a = 2 c % a = 1 a++ = 5 a– = 6 d++ = 33 ++d = 35     Relational Operators: When we want to relate or compare two values, we use relational operators in our Java code. Two operands are expected for the operator as inputs.The table given below will provide a detailed description of relational operators in Java. Assuming the values of variables are X = 20  & Y = 40.         Let’s look at a sample code that uses the above operators:     public class code {   public static void main (String [] args){       // operand varibale declaration and initialization.     int a = 5; int b = 22; int c = 16; int d = 33;       System.out.println( “a == b =” + (a == b));     System.out.println( “a != b =” + (a != b));     System.out.println( “a < b =” + (a < b));     System.out.println( “a > a =” + (a > a));     System.out.println( “b <= a =” + (b <= a));     System.out.println( “c >= a =” + (c >= a));   } }     After successful compilation, the above code will produce the following result:     Output:   a == b = false a != b = true a < b = true a > a = false b <= a = false c >= a = true     Bit Operator: Java defines bit operators for integer types (int), long integers (short), short integers (char), and byte types (byte).   Bit operators operate on all bits and perform bit-level operations. There are four bitwise operations and three bitshift operations that bit operators perform.   For example, if we take Bitwise AND operator which is a bitwise operator, the operation will be as follows:   Suppose X = 60 and Y = 13; their binary (bit) representation would be as follows:       X = 0011 1100 Y = 0000 1101     Now when we perform a Bitwise AND operation, ie. if both bits are 1, the result is 1 else it’s 0. So here the result will be:       X & Y = 0000 1100     The following table gives a summary of the available bitwise operators and bit shift operators in Java:       Logical Operators: The following table lists the basic operations of logical operators, assuming Boolean variable X is true and variable Y is false.         Assignment Operator: We have discussed this section in detail here: Java Assignment Operator     Other Operators:   Instanceof Operator: This operator is used to manipulate an object instance and check whether the object is a class type or interface type. The instanceof Java operator uses the following format:       (Object reference-variable) instanceof (class / interface type)     If the object pointed to by the variable on the left side of the operator is an object of the class or interface on the right side of the operator, the result is true. Below is an example:         Conditional Operator (?:): The conditional operator in Java is also called ternary operator. The ternary Java operator has 3 operands. The conditional operator is used to determine the boolean values. The main purpose of this operator is to finalize which value among the two should be assigned to a variable.   The syntax would be:       variable test_variable = (expression)? value if true : value if false;       Java operator precedence: When multiple operators are used in one Java statement, it is important to understand which one will act first. To solve this confusion, the Java language has something known as ‘operator precedence.’ This will allow the highest priority operators to act first and then the other operators act, following the precedence order. It’s important to understand that in a multi-operator expression, different operator precedence can lead to very different results.     End Note: We have learned about the different types of operators in Java language that are used for performing various mathematical operations. We also saw examples of  their syntax.   Besides Java Operators, you must also be well-versed with the concepts of Flow control statements and Assertion, Java collections and Stream, Threading in Java, etc. You can take a look at our blogs on these topics for in-depth understanding.     Get one step closer to your dream job!   Prepare for your Java programming interview with Core Java Questions You’ll Most Likely Be Asked. This book has a whopping 290 technical interview questions on Java Collections: List, Queues, Stacks, and Declarations and access control in Java, Exceptional Handling, Wrapper class, Java Operators, Java 8 and Java 9, etc. and 77 behavioral interview questions crafted by industry experts and interviewers from various interview panels. We’ve updated our blogs to reflect the latest changes in Java technologies. This blog was previously uploaded on March 31th, 2020, and has been updated on January 12th, 2022.
by Vibrant Publishers on May 20, 2022
by Vibrant Publishers on May 20, 2022
Like every other programming language, Java language also provides a feature called Operators. There are a few types of Operators exist in Java. In this java tutorial, we will focus our learning on Assignment operators. Assignment Operators exist for assigning a value to a variable in Java. The right side of the operator will be a value and the left side will be the variable that we created. So a general syntax would be as follows:       VARIABLE assignment operator VALUE ;     They are also known as Binary Operators because it requires two operands for it to work. Since there are many data types in java, one important thing here to remember is that the data type of the variable must match with the value that we assign. These variables can be a java array variable, a java object or any variable of a primitive data type.Now let’s look into multiple assignment operators that exist in Java language.     Simple Assignment Operator: As the name suggests this operator assigns values to variables.Syntax:       variable = value ;     Example:       string  name = “This is awesome!”;   A sample java code will be as given below:     Here if we examine the java class ‘SimpleAssignment’, the value assigned for java int variable ‘number’ is 10. And the java string variable ‘name’ has been assigned the value ‘This is awesome!”.Apart from simple assignment operations, java developers use this operator combined with other operators. This gives compound operators in the java code world. Let’s see those operators here:     += Assignment Operator: This is a combination of + and = java operators. So instead of writing,       number = number + 10;     we can combine the above statement in a java code as:     number += 10;   For Example:The given java code blocks for java compiler will be the same and will produce the same output ie. 13.2       float java = 1.00; java +=12.2; float java = 1.00; java = java + 12.2;     -= Assignment Operator: Similar to the compound statement that we saw above, here also we combine ‘-’ and ‘=’ java operators. So here the value on the right side is subtracted from the initial value of the variable and it is assigned to the variable itself.Syntax:       numberA -= numberB ;     Example:       numberA -= 10; this means numberA = numberA -10;     *= Assignment Operator: In this case, we combine ‘*’ and ‘=’ java operators. So here the value on the right side is multiplied with the initial value of the variable and it is assigned to the variable itself.Syntax:       numberA *= numberB ;     Example:       numberA *= 10; this means numberA = numberA *10;     /= Assignment Operator: Just like the above cases,  here we combine ‘/’ and ‘=’ java operators. In this operation, the initial value of the variable on the left side is divided with the value on the right side and the resulting quotient is assigned to the left side variable itself.Syntax:       numberA /= numberB ;     Example:       numberA /= 10; this means numberA = numberA /10;     Summary: The following table provides a summary of the Assignment Operators in Java Language.     For a java developer, it’s important to understand the usage of Operators. If you want to learn java programming, then you must know these concepts well.
by Vibrant Publishers on May 20, 2022