๐ WAYPOINT LESSON
Precedence and compound assignment
โญ beginnerโณ 10 min read๐ Lesson 11 of 256
The full operator ladder and += -= *= /= %=.
Compound assignment: x += 5 means x = x + 5. Same for -=, *=,
/= and %=. One subtlety: x += 2.5 compiles even when x is an int
(the compound form contains a hidden cast) โ but plain x = x + 2.5 does
not. Prefer the explicit cast for clarity; know the rule for tracing.
Operator precedence ladder (high to low):
++ -- !(unary)* / %+ -- relational
< > <= >= - equality
== != &&||- assignment
= += -= ...
Two classics:
System.out.println(3 < 5 == true); // true โ relational before equality
boolean ok = 5 > 3 && 2 > 1 || 1 > 2; // (true && true) || false โ true
When in doubt (in reading or writing), use parentheses. The exam expects you to evaluate by the ladder, but human readers thank you for clarity.