Java's operators are C-like, with a few Java-specific twists: >>>, string +, instanceof, and strict boolean conditions.
| Kind | Operators | Notes |
|---|---|---|
| Arithmetic | + - * / % | Integer / truncates toward zero; % takes the sign of the dividend |
| Unary | + - ++ -- !, ~ | Prefix ++i increments then yields; postfix i++ yields then increments |
| Relational | == != < <= > >= | == on objects compares references |
| Logical | && || ! | Short-circuit; operands must be boolean |
| Bitwise | & | ^ ~ | On boolean operands &/| evaluate both sides |
| Shift | << >> >>> | >> keeps the sign, >>> fills with zeros |
| Assignment | = += -= *= /= %= &= |= ^= <<= >>= >>>= | Compound forms cast implicitly |
| Ternary | cond ? a : b | An expression — yields a value |
| Type | instanceof | obj instanceof String s binds a variable (Java 16+) |
Arithmetic details
public class Main {
public static void main(String[] args) {
System.out.println(7 / 2 + " " + -7 / 2 + " " + 7 % 3 + " " + -7 % 3);
System.out.println(Math.floorDiv(-7, 2) + " " + Math.floorMod(-7, 3));
System.out.println(5.5 % 2);
int i = 5;
int a = i++; // a = 5, then i = 6
int b = ++i; // i = 7, then b = 7
System.out.println(a + " " + b + " " + i);
String s = "Sum: " + 1 + 2; // left to right: string concat
String t = 1 + 2 + " is the sum"; // 3 first, then concat
System.out.println(s + " | " + t);
}
}3 -3 1 -1
-4 2
1.5
5 7 7
Sum: 12 | 3 is the sumShort-circuit evaluation
public class Main {
static boolean check(String label, boolean value) {
System.out.println("evaluating " + label);
return value;
}
public static void main(String[] args) {
String s = null;
if (s != null && s.length() > 3) System.out.println("long"); // safe: right side skipped
System.out.println(check("A", false) && check("B", true));
System.out.println(check("C", false) & check("D", true)); // non-short-circuit
}
}evaluating A
false
evaluating C
evaluating D
falseBitwise & shift operators
public class Main {
public static void main(String[] args) {
int a = 0b1100, b = 0b1010;
System.out.println((a & b) + " " + (a | b) + " " + (a ^ b) + " " + ~a);
System.out.println((1 << 10) + " " + (-16 >> 2) + " " + (-16 >>> 28));
int n = 12;
System.out.println("even? " + ((n & 1) == 0));
System.out.println("power of two? " + (n > 0 && (n & (n - 1)) == 0));
System.out.println("set bits: " + Integer.bitCount(n));
int x = 3, y = 9;
x ^= y; y ^= x; x ^= y; // XOR swap
System.out.println(x + " " + y);
}
}8 14 6 -13
1024 -4 15
even? true
power of two? false
set bits: 2
9 3Precedence (high → low)
| Level | Operators |
|---|---|
| Postfix | expr++ expr-- |
| Unary | ++expr --expr + - ~ ! and casts |
| Multiplicative | * / % |
| Additive | + - |
| Shift | << >> >>> |
| Relational | < > <= >= instanceof |
| Equality | == != |
| Bitwise | & then ^ then | |
| Logical | && then || |
| Ternary | ? : |
| Assignment | = += -= … (right-associative) |
public class Main {
public static void main(String[] args) {
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
System.out.println(1 + 2 << 1); // + binds tighter than <<
System.out.println(true || false && false); // && before ||
int score = 72;
String grade = score >= 90 ? "A" : score >= 70 ? "B" : "C";
System.out.println(grade);
}
}14
20
6
true
BWhat's the difference between & and &&?
What is >>>?
What does System.out.println(1 + 2 + "3" + 4 + 5) print?