Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
Arithmetic Operators
Arithmetic operators are used in mathematical like addition, subtraction etc. The following table lists the arithmetic operators:
Assume that int X = 10 and int Y = 20
Operators | Description |
|---|---|
+ | Addition – Adds values on either side of the operator |
- | Subtraction – Subtracts right hand operand from left hand operand |
* | Multiplication – Multiplies values on either side of the operand |
/ | Division - Divides left hand operand by right hand operand |
% | Modulus - Divides left hand operand by right hand operand and returns remainder |
++ | Increment - Increase the value of operand by 1 |
-- | Decrement - Decrease the value of operand by 1 |
Example:
Relational Operators
public class Main{
public static void main(String args[]{
Int X = 10;
Int Y = 20;
System.out.println("Addition (X+Y) = "+(X+Y)); // return 30
System.out.println("Subtraction (X-Y) = "+(X-Y)); // return -10
System.out.println("Multiplication (X*Y) = "+(X*Y)); // return 200
System.out.println("Division (Y/X) = "+(Y/X)); // return 2
System.out.println("Addition (Y%X) = "+(Y%X)); // return 0
Y++;
System.out.println("Increment Y = "+Y); // return 21
X--;
System.out.println("Decrement X = "+X); // return 9
}
}
There are following relational operators supported by Java language like ==, ! = etc.
Assume variable X=10 and variable Y=20 then:
Operator | Description |
|---|---|
== | Checks if the value of two operands are equal or not, if yes then condition becomes true. |
!= | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. |
Example :
public class Main{
public static void main(String args[]){
int X = 10;
int Y = 20;
System.out.println("(X == Y) = "+(X == Y));
System.out.println("(X != Y) = "+(X != Y));
System.out.println("(X > Y) = "+(X > Y));
System.out.println("(X < Y) = "+(X < Y));
System.out.println("(X >= Y) = "+(X >= Y));
System.out.println("(X <= Y) = "+(X <= Y));
}
}
Bitwise Operators
Java defines several bitwise operators like &, | etc which can be applied to the integer types(long, int, short, char, and byte).
Bitwise operator works on bits(0 or 1) and perform bit by bit operation. Assume if x = 60; and y = 13; Now in binary format they will be as follows:
x = 0011 1100
y = 0000 1101
-----------------
x&y = 0000 1100
x|y = 0011 1101
x^y = 0011 0001
~x = 1100 0011
The following table lists the bitwise operators:
Assume integer variable X=60 and variable Y=13 then:
Operator | Description |
|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. |
| | Binary OR Operator copies a bit if it exists in eather operand. |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. |
~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. |
>>> | Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. |
Example :
public class Main{
public static void main(String args[]){
int X = 60;
int Y = 13;
System.out.println("(X & Y) = "+(X & Y));
System.out.println("(X | Y) = "+(X | Y));
System.out.println("(X ^ Y) = "+(X ^ Y));
System.out.println("(~X) = "+(~X));
System.out.println("(X << Y) = "+(X << 2));
System.out.println("(X >> Y) = "+(X >> 3));
System.out.println("(X >>> Y) = "+(X >>> 1));
}
}
Logical Operators
The following table lists the logical operators like &&, || etc. This logical operator use for join two condition.
Assume boolean variables X=true and variable Y=false then:
Operator | Description |
|---|---|
&& | Called Logical AND operator. If both the operands are non zero then then condition becomes true. |
|| | Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
Example:
public class Main{
public static void main(String args[]){
int X = 60;
int Y = 13;
if((X == Y) && (X != Y)){
System.out.println("True");
}else{
System.out.println("False");
}
if((X == Y) || (X != Y)){
System.out.println("True");
}
else{
System.out.println("False");
}
}
}
Assignment Operators
There are following assignment operators supported by Java language:
Operator | Description |
|---|---|
= | Simple assignment operator, Assigns values from right side operands to left side operand |
+= | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand |
-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand |
*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand |
/= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand |
%= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand |
<<= | Left shift AND assignment operator |
>>= | Right shift AND assignment operator |
&= | Bitwise AND assignment operator |
^= | Bitwise exclusive OR and assignment operator |
|= | Bitwise inclusive OR and assignment operator |
Example:
public class Main{
public static void main(String args[]){
int X = 60;
int Y = 13;
X += 1;
System.out.println("X+=1 : "+X);
Y<<=1;
System.out.println("Y<<=1 : "+Y);
/* Return 26 : 13(binary - 00001101) shift one bit left means 26(00011010) */
}
}