Fundamentals · Medium · Free
Explain operator precedence and associativity with a tricky expression example.
FresherFundamentalsProgramming Fundamentals
Precedence and associativity
Precedence decides which operators bind tighter (* before +). Associativity decides grouping among equal precedence—most binary ops are left-associative; assignment is right-associative.
java
int x = 2 + 3 * 4; // 14, not 20
boolean ok = a > 0 && b < 10; // && lower than comparisons
int y = a = b = 1; // right-associative assignment- Unary
!and casts bind tighter than arithmetic. - Logical
&&/||short-circuit—second operand may not run. - When unsure, use parentheses—clarity beats cleverness in interviews.
Walk through one expression line-by-line on the whiteboard—shows careful thinking.