Java Operators are symbols that perform operations on variables and constants. Java Operators can be divided into the following types:
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- MISC Operators
Arithmetic Operators
Arithmetic Operators are the symbols that perform mathematical operations on variables and constants. The below table shows a list of arithmetic operators in Java and their description. Consider two values A and B having values 20 and 40 respectively.
Sr.No. | Operator | Name | Description | Example |
1. | + | Addition | Add two values. | A+B is equal to 60 |
2. | – | Subtraction | Subtracts two values. | B – A is equal to 20 |
3. | * | Multiplication | Multiplies two values. | A*B is equal to 800 |
4. | / | Division | Divides two values. | B/A is equal to 2 |
5. | % | Modulus | Takes modulus of two values. | B%A is equal to 0 |
6. | ++ | Increment | Increases the value by 1. | A++ is equal to 21 |
7. | ++ | Decrement | Decreases the value by 1. | A– is equal to 19 |
The below code shows how to use arithmetic operators in Java code:
public class MyProgram { public static void main(String[] args) { int a = 20; int b = 40; int add = a + b; int sub = b - a; int pro = a*b; int div = b/a; int mod = b%a; int inc = ++a; int dec = --a; System.out.println("a + b is: " + add); System.out.println("b - a is: " + sub); System.out.println("a*b is: " + pro); System.out.println("b/a is: " + div); System.out.println("b%a is: " + mod); System.out.println("++a is: " + inc); System.out.println("--a is: " + dec); } }
The output of the above program after compilation is:
Relational Operators
Relational Java Operators are also called as Comparison Operators. These operators compare two values and return a boolean value either true or false. The table below shows the list of relational operators and their description. Consider two variables A and B having values 10 and 20 respectively.
Sr.No. | Operator | Name | Description | Example |
1. | == | Equals to | Compares two values and returns true if they are equal, otherwise returns false. | A==B gives false |
2. | != | Not equal to | Compares two values and returns false if they are equal, otherwise returns true. | A!=B gives true |
3. | > | Greater than | Compares two operands and returns true if left side operand is greater than right side operand otherwise returns false. | A>B gives false |
4. | < | Smaller than | Compares two operands and returns true if left side operand is smaller than right side operand otherwise returns false. | A<B gives true |
5. | >= | Greater than or equal to | Compares two operands and returns true if left side operand is greater than or equal to right side operand otherwise returns false. | A>=B gives false |
6. | <= | Smaller than or equal to | Compares two operands and returns true if left side operand is smaller than or equal to right side operand otherwise returns false. | A<=B gives true |
The below code shows the use of relational operators:
public class MyProgram { public static void main(String[] args) { int a = 10; int b = 20; System.out.println("a == b is: " + (a == b)); // returns false because 10 is not equal to 20 System.out.println("a != b is: " + (a != b)); // returns true because 10 is not equal to 20 System.out.println("a > b is: " + (a > b)); // returns false because 10 is not greater than 20 System.out.println("a < b is: " + (a < b)); // returns true because 10 is smaller than 20 System.out.println("a >= b is: " + (a >= b)); // returns false because 10 is not greater than or equal to 20 System.out.println("a <= b is: " + (a <= b)); // returns true because 10 is smaller than or equal to 20 } }
The output of the above code after compilation is:
Bitwise Operators
Bitwise operators perform bitwise operations on numbers (int, long, short, char, byte) and characters (char). Java provides bitwise operators (&, |, ^, ~, <<, >>) that perform bit by bit operations on binary digits. Consider two variables A and B having values 10 and 15 respectively. Their binary values will be:
A = 1010 equals 10
B = 1111 equals 15
The result of bitwise operations on A and B will be:
A&B = 1010 equals 10
A|B = 1111 equals 15
A^B = 101 equals 5
~A = -1011 equals -11
The table below shows a list of bitwise operators in java and their description with examples:
Sr.No. | Operator | Name | Description | Example |
1. | & | AND | Performs bitwise AND on two operands. It copies a bit to the result if it is present in both operands. | A&B gives 10 which is 1010 |
2. | | | OR | Performs bitwise OR on two operands. It copies a bit to the result if it is present in one of the two operands. | A|B gives 15 which is 1111 |
3. | ^ | XOR | Performs bitwise XOR on two operands. It copies a bit to the result if it is in only one of the two operands. | A^B gives 5 which is 0101 |
4. | ~ | Compliment | Performs binary bitwise Compliment. It changes each bit to its opposite. | ~A gives -11 which is -1011 |
5. | << | Shift Left | Bitwise Shift Left operator moves bits of the left side operand to left by the number of bits in the right operand. | A<<2 gives 40 which is 0010 1000 |
6. | >> | Shift Right | Bitwise Shift Right operator moves bits of the left side operand to right by the number of bits in the right operand. | A>>2 gives 2 which is 0010 |
7. | >>> | Zero Fill Shift Right | Bitwise Zero Fill Shift Right operator moves bits of the left side operand to right by the number of bits in the right operand and fills the shifted values with zeros. | A>>>2 gives 2 which is 0010 |
public class MyProgram { public static void main(String args[]) { int a = 10; /* 10 = 1010 */ int b = 15; /* 15 = 1111 */ int c = 0; c = a & b; /* 10 = 1010 */ System.out.println("a & b = " + c ); c = a | b; /* 15 = 1111 */ System.out.println("a | b = " + c ); c = a ^ b; /* 5 = 0101 */ System.out.println("a ^ b = " + c ); c = ~a; /*-11 = -1011 */ System.out.println("~a = " + c ); c = a << 2; /* 40 = 0010 1000 */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 2 = 0010 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 2 = 0010 */ System.out.println("a >>> 2 = " + c ); } }
The output of the above code after compilation is:
Logical Operators
The below table shows the list of logical operators in Java. These operators return a boolean value. Consider two boolean variables A and B. A has true value and B has false. The result of logical operators on boolean variables A and B are shown in the table below:
Sr.No. | Operator | Name | Description | Example |
1. | && | Logical AND | Compares two statements and returns true if both are true. | A&&B gives false |
2. | || | Logical OR | Compares two statements are returns true if either of the two statements is true. | A||B gives true |
3. | ! | Logical NOT | Logical NOT operator reverses the boolean value. It converts true value to false and false to true. | !A gives false |
public class MyProgram { public static void main(String args[]) { boolean a = true; boolean b = false; System.out.println("a && b is " + (a&&b)); System.out.println("a || b is " + (a||b) ); System.out.println("!(a && b) is " + !(a && b)); } }
The output of the above code after compilation is:
Assignment Operators
Assignment Operators assign values to variables. The table below shows list of assignment operators and their description with examples.
Sr.No. | Operator | Name | Description | Example |
1. | = | Assignment | Assigns value from right side operand to the left side operand. | A=B will assign the value in B to A. |
2. | += | Add AND assignment | Adds right and left side operands and copies the value to the left side operand. | A+=B is same as A= A+B |
3. | -= | Subtract AND assignment | Subtracts right side operand from left side operand and copies the value to the left side operand. | A-=B is same as A= A-B |
4. | *= | Multiply AND assignment | Multiplies right and left side operands and assigns the value to the left side operand. | A*=B is same as A=A*B |
5. | /= | Divide AND assignment | Divides left side operand with right side operand and assigns the value to the left operand. | A/=B is same as A=A/B |
6. | %= | Modulus AND assignment | It takes modulus of left side and right side operands and assigns value to the left operand. | A%=B is same as A= A%B |
7. | <<= | Left shift AND assignment | It performs Left Shift and assigns value to the left side operand. | A <<= 2 is same as A = A << 2 |
8. | >>= | Right shift AND assignment | It performs Right Shift and assigns value to the left side operand. | A >>= 2 is same as A = A >> 2 |
9. | &= | Bitwise AND assignment | It performs Bitwise AND and assigns value to the left side operand. | A &= 2 is same as A = A & 2 |
10. | ^= | Bitwise exclusive OR and assignment | It performs Bitwise XOR and assigns value to the left side operand. | A ^= 2 is same as A = A ^ 2 |
11. | |= | Bitwise inclusive OR and assignment | It performs Bitwise OR and assigns value to the left side operand. | A |= 2 is same as A = A | 2 |
Miscellaneous Operators
The short form of Miscellaneous Operators is MISC operators. Java supports the following MISC operators:
Ternary Operators
Ternary Operator is also known as Conditional Operators. These operators are a combination of three operators and evaluates Boolean expressions. The conditional operator assigns value depending on the boolean value of the expression. The syntax is:
The below code demonstrates the concept of conditional operator.
public class MyProgram { public static void main(String args[]) { int x, y; x = 10; y = (x == 1) ? 15: 20; System.out.println( "Value of y is : " + y ); y = (x == 10) ? 15: 20; System.out.println( "Value of y is : " + y ); } }
The output of the above program after compilation is: