Operators
Operators are symbols that perform operations on variables and values.
Operators in Java can be classified into 6 categories:
- Arithmetic Operators
- Assignment Operators
- Comparison/Relational Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
Arithmetic Operators
Section titled “Arithmetic Operators”Arithmetic operators are used to perform arithmetic operations on variables and data.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
++ | Increment | a++ or ++a |
-- | Decrement | a-- or --a |
int sum = 5 + 3; // 8int diff = 5 - 3; // 2int product = 5 * 3; // 15int quotient = 5 / 2; // 2 (integer division)double exactQuotient = 5.0 / 2.0; // 2.5int remainder = 5 % 2; // 1
int a = 5;a++; // a is now 6++a; // a is now 7 (prefix: increment then use)a--; // a is now 6Assignment Operators
Section titled “Assignment Operators”Assignment operators are used in Java to assign values to variables.
| Operator | Description | Example | Equivalent to |
|---|---|---|---|
= | Assignment | a = b | a = b |
+= | Add and assign | a += b | a = a + b |
-= | Subtract and assign | a -= b | a = a - b |
*= | Multiply and assign | a *= b | a = a * b |
/= | Divide and assign | a /= b | a = a / b |
%= | Modulus and assign | a %= b | a = a % b |
int x = 10;x += 5; // x = 15x -= 3; // x = 12x *= 2; // x = 24x /= 3; // x = 8x %= 5; // x = 3Comparison/Relational Operators
Section titled “Comparison/Relational Operators”Relational operators are used to check the relationship between two operands.
| Operator | Description | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
int a = 5;int b = 7;boolean isEqual = (a == b); // falseboolean isNotEqual = (a != b); // trueboolean isGreater = (a > b); // falseboolean isLess = (a < b); // trueboolean isGreaterOrEqual = (a >= b); // falseboolean isLessOrEqual = (a <= b); // trueLogical Operators
Section titled “Logical Operators”Logical operators are used to check whether an expression is true or false.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | Logical NOT | !a |
boolean a = true;boolean b = false;
boolean andResult = a && b; // falseboolean orResult = a || b; // trueboolean notResult = !a; // falseUnary Operators
Section titled “Unary Operators”Unary operators are used with only one operand.
Bitwise Operators
Section titled “Bitwise Operators”Bitwise operators in Java are used to perform operations on individual bits.
| Operator | Description | Example |
|---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << n |
>> | Right shift | a >> n |
>>> | Unsigned right shift | a >>> n |
int a = 5; // 0101 in binaryint b = 3; // 0011 in binary
int bitwiseAnd = a & b; // 0001 -> 1int bitwiseOr = a | b; // 0111 -> 7int bitwiseXor = a ^ b; // 0110 -> 6int bitwiseNot = ~a; // 1010 -> -6 (in two's complement)int leftShift = a << 1; // 1010 -> 10int rightShift = a >> 1; // 0010 -> 2