After acquiring the knowledge about the various Data Types available in Java, Lets learn which all operations can we perform on the different variables in Java, as what is the use of such learning, where you don't even know, where and how to implement that.
An Operator is a character which tells the computer, which kind of operation is to be performed the operands(variables) provided to it. For example, + , - , * , / etc. In Java Programming, we mainly have 5 groups of Operators, namely:
We'll be discussing one by one...
Assignment Operators
Arithmetic Operators are used to perform mathematical operations on the various expressions. For example:
int a,b;
int c = a+b;
System.out.println(c);
Here, + is the arithmetic operator, which performs addition operation on the variables a and b, and then stores the result in variable c.
Various Arithmetic Operators are mentioned in the table below:
* In case of Increment and Decrement Operators, there are 2 types: Post-Increment/Decrement and Pre-Increment/Decrement. These are the known as the Unary Operators, which operate on only 1 operand. As the name suggests in Post-increment, the signs come after the operand. Like a++ or a--, whereas in Pre-increment/decrement, the symbols come before the operand. For example: ++a and --a. Both do the same task, of increment or decrement the value by one, like if a=3, and we perform a++, then a would become 4. The difference lies when they are used in expressions. Let's see the difference between them, by the help of a program...
class operators
{
public static void main(String arg[])
{
int a = 5;
int b = 2;
int c;
int d;
c = ++b; //c becomes 3 and b also is 3
d = a++; //d becomes 5 whereas a is 6
c++; //c becomes 4
System.out.println("a = " + a + "\n" + " b = " + b + "\n" + " c = " + c + "\n" + " d = " + d);
}
}
OUTPUT
a = 6
b = 3
c = 4
d = 5
* Explanation: At c = ++b ; firstly and increment is made in b, and then it is stored in c, where in d = a++, firstly the initial value of a, i.e 5 is stored into d and then the value of a is incremented.
Program:
public class arithmeticOperators {
public static void main(String[] args) {
int a=3;
int b=4;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
//Post and Pre Increment Operators
System.out.println(a++);
//this statement prints a=3 and then increments a to 4
// Print(a) --> 3 , then a++ so now a = 4
System.out.println(++a);
//this statement firstly increments a, and then prints the value of a
// Print(++a) --> firstly ++a, ++4, which is 5, then a = 5, then print a
//Post and Pre Decrement Operators
System.out.println(b--);
System.out.println(--b);
}
}
OUTPUT
7
-1
12
0
3
3
5
4
2
Assignment Operators
This is the most commonly used operator, in any language, which uses the = sign, to assign value on the right side to the variable on the left side.
Example:
int a = 2;
Comparison Operators
These are also known as Relational Operators, being used to signify the relationship between two or more variables.
public class largestOfThreeNumberUsingIfElse {
public static void main(String[] args) {
int a = 12;
int b = 13;
int c = 14;
if(a>b)
{
if(a>c)
System.out.println(a);
else
System.out.println(c);
}
else
{
if(b>c)
System.out.println(b);
else
System.out.println(c);
}
}
}
OUTPUT
14
Logical Operators
These are mainly used when we have to check whether both expressions are true, either 1 is true or any such case. These are of 3 types:
Program:
public class LogicalOperators {
public static void main(String args[]) {
boolean b1 = true;
boolean b2 = false;
System.out.println("b1 && b2: " + (b1&&b2));
System.out.println("b1 || b2: " + (b1||b2));
System.out.println("!(b1 && b2): " + !(b1&&b2));
}
}
OUTPUT
b1 && b2: false
b1 || b2: true
!(b1 && b2): true
Bit-wise Operators
These operators are a special set of operators found in every programming language. These are mostly used on bits or in bit-manipulation. These are the same as the ones we studied in C programming - Bitwise Operators
Bit-wise AND Operator ( & )
This operator take 2 binary numbers and perform AND operation on them, this is like the multiplication operation performed in maths. The output of the operation on 2 bits is as follows:
* Here the result(R) is 1 if both the corresponding bits of the operands are 1, otherwise if either bit of any operand is 0, then the result/output would be 0.
Example :
10 in binary is 1010 and 9 is 1001, if we perform & operation on them, we'll get 1000
which is the decimal equivalent of 8. Hence, 10 & 9 = 8
Bit-wise OR Operator ( | )
It also take 2 operands, and do a operation similar to the Addition operation in Mathematics, just with a little difference.
* Here the result is 1, when any of corresponding bits of either operands is 1, but if both the bits are 0, then the output is zero.
Example :
Let us again consider the above two numbers, 10 and 9, i.e 1010 and 1001 in binary representation, now the output would be 1011
which is the decimal equivalent of 11. Hence 10 | 9 = 11
Bit-wise XOR Operator ( ^ )
It is also known as the Exclusive OR operator. Its output is 1 when the corresponding bits are opposite in nature like 0-1 or 1-0, and if the corresponding bits are same, then the output is 0.
Example :
10 ^ 9 = 1010 ^ 1001 = 0011
which is the decimal equivalent of 3
Bit-wise NOT Operator ( ~ )
It is a unary operator, i.e it works on only 1 operand, and flip each bit of the number. Meaning if the bit is 1 it flips it to 0, and 0 to 1. This is known as the Logical negation of bits.
Example :
~ ( 9 ) = 6 , This negation compliment obtained is known as 1's Complement. There is an another type of compliment known as 2's Compliment, where negation is done 1 added to the 1's compliment found.
Hence 2's compliment of X = - ( n + 1 ).
2's complement of 7 (0111) is 9 (1001)
Bit-wise Right Shift Operator ( >> )
The Bit-wise Right Shift Operator, right shifts all the bits of the operand placed at the left side, in accordance to the value on the right side, which tells us the number of positions to shift.
Syntax: x >> y, which is similar to dividing x with 2 raise to power y.
Example 1 :
5 >> 1, hence we have to shift the bits of 0101(binary representation of 5), 1 position to the right.
* On shifting the bits to right, the empty positions on the left side will be occupied by 0s.
So 5 >> 1 = 2, which is decimal representation of 0010.
Example 2 :
5 >> 2
0101 >> 2 = 0001
Hence, 5 >> 2 = 1
Bit-wise Left Shift Operator ( << )
The Bit-wise Left Shift Operator, left shifts all the bits of the operand placed on the left side, in accordance to the value on the right side.
Syntax : x << y, this expression shifts the bits of x, y places to the left. And the empty positions are occupied by 0s.
* x << y is equivalent to multiplying x with 2 raise to power y.
Example 1 :
5 << 1, will shift the bits of 5(0101) to the left by 1 place.
which is equivalent to 10 in decimal format.
Example 2 :
5 << 2
0101 << 2 = 0100
Hence, 5 << 2 = 20 // 0000 0101 << 2 = 0001 0100
Program:
public class BitwiseOperator {
public static void main(String args[]) {
int num1 = 11; /* 11 = 00001011 */
int num2 = 22; /* 22 = 00010110 */
int result = 0;
result = num1 & num2;
System.out.println("num1 & num2: "+result);
result = num1 | num2;
System.out.println("num1 | num2: "+result);
result = num1 ^ num2;
System.out.println("num1 ^ num2: "+result);
result = ~num1;
System.out.println("~num1: "+result);
result = num1 << 2;
System.out.println("num1 << 2: "+result); result = num1 >> 2;
System.out.println("num1 >> 2: "+result);
}
}
OUTPUT
num1 & num2: 2
num1 | num2: 31
num1 ^ num2: 29
~num1: -12
num1 << 2: 44 num1 >> 2: 2