| Group | Operators |
|---|---|
| Postfix | expr++ expr-- |
| Unary | ++expr --expr + - ~ ! |
| Multiplicative | * / % |
| Additive | + - |
| Shift | << >> >>> |
| Relational | < > <= >= instanceof |
| Equality | == != |
| Bitwise AND / XOR / OR | & ^ | |
| Logical | && || |
| Ternary / assignment | ? : = += … (right-associative) |
Parenthesize mixed shifts, bitwise, and comparisons. Left-to-right evaluation is not the same as precedence.
long y = 2_000_000_000 * 2 overflows as int. Use L./ toward zero. % sign follows the dividend. / 0 on int throws; on double is Infinity.+ becomes concat if either side is String: "id=" + 1 + 2 → id=12.| Want | Use |
|---|---|
| Guard a call / avoid NPE | && || |
| Both sides always run | & | on boolean (rare) |
| Flags / masks | & | ^ ~ << >> >>> on ints |
| Identity | == on references |
| Content | equals |
if (s != null && s.length() > 0) { }
int canRead = flags & READ;
n++ yields old then adds. ++n adds then yields new.++ in a larger expression.b += 1 on byte includes a hidden cast. b = b + 1 does not compile.~ Good luck with your revision! ~