Java Operators
Arithmetic Operators
| Operator | Name | Example | Result |
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | Division | 10 / 3 | 3 (integer division) |
| % | Modulus | 10 % 3 | 1 |
| ++ | Increment | a++ | a = a + 1 |
| -- | Decrement | a-- | a = a - 1 |
Relational & Logical Operators
| Category | Operator | Meaning |
| Relational | == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
| Logical | && | AND — true if both operands are true |
| || | OR — true if at least one operand is true |
| ! | NOT — inverts the boolean value |
Arithmetic, Relational & Logical
public class Operators {
public static void main(String[] args) {
int a = 10, b = 3;
// Arithmetic
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3 (integer division)
System.out.println(a % b); // 1
// Relational
System.out.println(a > b); // true
System.out.println(a == b); // false
// Logical
boolean x = true, y = false;
System.out.println(x && y); // false
System.out.println(x || y); // true
System.out.println(!x); // false
}
}
Bitwise Operators
| Operator | Name | Example (a=5, b=3) | Result |
| & | Bitwise AND | 5 & 3 → 0101 & 0011 | 1 (0001) |
| | | Bitwise OR | 5 | 3 → 0101 | 0011 | 7 (0111) |
| ^ | Bitwise XOR | 5 ^ 3 → 0101 ^ 0011 | 6 (0110) |
| ~ | Bitwise NOT | ~5 | -6 |
| << | Left shift | 5 << 1 | 10 |
| >> | Right shift | 5 >> 1 | 2 |
Ternary & instanceof Operators
Ternary, Assignment & instanceof
public class MoreOperators {
public static void main(String[] args) {
// Ternary: condition ? valueIfTrue : valueIfFalse
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status); // Adult
// Compound assignment operators
int n = 10;
n += 5; // n = n + 5 → 15
n -= 3; // n = n - 3 → 12
n *= 2; // n = n * 2 → 24
n /= 4; // n = n / 4 → 6
n %= 4; // n = n % 4 → 2
System.out.println("n = " + n); // 2
// instanceof — checks if an object is an instance of a class
String str = "Hello";
System.out.println(str instanceof String); // true
System.out.println(str instanceof Object); // true (String extends Object)
Object obj = "World";
if (obj instanceof String s) { // pattern matching (Java 16+)
System.out.println("Length: " + s.length());
}
}
}