Java Operator Classification
The following are common operators in Java along with examples:
1. Arithmetic Operators
Addition Operator (+):
int a = 5;
int b = 3;
int c = a + b; // The value of c is 8
Subtraction Operator (-):
int a = 5;
int b = 3;
int c = a - b; // The value of c is 2
Multiplication Operator (*):
int a = 5;
int b = 3;
int c = a * b; // The value of c is 15
Division Operator (/):
int a = 5;
int b = 3;
int c = a / b; // The value of c is 1
Modulo Operator (%):
int a = 5;
int b = 3;
int c = a % b; // The value of c is 2
2. Assignment Operators
Equality Operator (=):
int a = 5;
Add-and-Assign Operator (+=):
int a = 5;
a += 3; // The value of a is 8
Subtract-and-Assign Operator (-=):
int a = 5;
a -= 3; // The value of a is 2
Multiply-and-Assign Operator (*=):
int a = 5;
a *= 3; // The value of a is 15
Divide-and-Assign Operator (/=):
int a = 5;
a /= 3; // The value of a is 1
Modulo-and-Assign Operator (%=):
int a = 5;
a %= 3; // The value of a is 2
3. Relational Operators
Greater Than Operator (>):
int a = 5;
int b = 3;
boolean c = a > b; // The value of c is true
Less Than Operator (<):
int a = 5;
int b = 3;
boolean c = a < b; // The value of c is false
Greater Than or Equal To Operator (>=):
int a = 5;
int b = 3;
boolean c = a >= b; // The value of c is true
Less Than or Equal To Operator (<=):
int a = 5;
int b = 3;
boolean c = a <= b; // The value of c is false
Equality Operator (==):
int a = 5;
int b = 5;
boolean c = a == b; // The value of c is true
Inequality Operator (!=):
int a = 5;
int b = 3;
boolean c = a != b; // The value of c is true
4. Logical Operators
AND Operator (&&):
int a = 5;
int b = 3;
boolean c = (a > b) && (a < 10); // The value of c is true
OR Operator (||):
int a = 5;
int b = 3;
boolean c = (a < b) || (a < 10); // The value of c is true
NOT Operator (!):
int a = 5;
boolean c = !(a > 10); // The value of c is true
5. Bitwise Operators
Bitwise AND Operator (&):
int a = 5;
int b = 3;
int c = a & b; // The value of c is 1
Bitwise OR Operator (|):
int a = 5;
int b = 3;
int c = a | b; // The value of c is 7
Bitwise XOR Operator (^):
int a = 5;
int b = 3;
int c = a ^ b; // The value of c is 6
Shift Operators (<<, >>, >>>):
int a = 5;
int b = a << 2; // The value of b is 20, shifting a left by two bits
int c = a >> 1; // The value of c is 2, shifting a right by one bit
int d = a >>> 1; // The value of d is 2, shifting a unsigned right by one bit
6. Conditional Operators
Ternary Operator (? :)
int a = 5;
int b = 3;
int c = (a > b) ? a : b; // The value of c is 5; if a > b, c equals a, otherwise it equals b