Operator Precedence and Associativity
In school math you learned BODMAS: brackets first, then division and multiplication, then addition and subtraction. Java follows the same idea, with more operators. Precedence decides which operator runs first when different operators meet. Associativity decides the order when operators of the same level sit side by side. With both, you can evaluate any Java expression on paper.
Higher Precedence Runs First
public class Order {
public static void main(String[] args) {
int a = 2 + 3 * 4 - 6 / 2;
int b = (2 + 3) * 4;
boolean c = 5 + 3 > 7 && 10 % 3 == 1;
System.out.println(a + " " + b + " " + c);
}
}11 20 trueHow it works
- Line 3:
*and/come before+and-. So3 * 4 = 12and6 / 2 = 3first, then2 + 12 - 3 = 11. - Line 4: brackets come first, so
(2 + 3) = 5, then5 * 4 = 20. - Line 5: arithmetic, then comparison, then
&&.5 + 3 > 7is8 > 7, which istrue;10 % 3 == 1is1 == 1, which istrue;true && trueistrue.
Try it yourself: work out 10 - 4 - 3 and 100 / 10 / 5 by hand before running them. Did you go left to right?
The Precedence Table
From highest (runs first) to lowest:
| Level | Operators | Associativity |
|---|---|---|
| 1 | Postfix: x++ x-- | Left to right |
| 2 | Unary: ++x --x + - ! ~ (type) | Right to left |
| 3 | Multiplicative: * / % | Left to right |
| 4 | Additive: + - | Left to right |
| 5 | Shift: << >> >>> | Left to right |
| 6 | Relational: < > <= >= instanceof | Left to right |
| 7 | Equality: == != | Left to right |
| 8 | Bitwise AND: & | Left to right |
| 9 | Bitwise XOR: ^ | Left to right |
| 10 | Bitwise OR: | | Left to right |
| 11 | Logical AND: && | Left to right |
| 12 | Logical OR: || | Left to right |
| 13 | Ternary: ? : | Right to left |
| 14 | Assignment: = += -= and friends | Right to left |
Associativity: Same Level, Which Way?
When operators of the same level meet, associativity decides:
| Expression | Associativity | Steps | Result |
|---|---|---|---|
10 - 4 - 3 | Left to right | (10 - 4) - 3 | 3 |
100 / 10 / 5 | Left to right | (100 / 10) / 5 | 2 |
a = b = c = 7 | Right to left | a = (b = (c = 7)) | all three are 7 |
!!true | Right to left | !(!true) | true |
Most operators go left to right. Unary operators, the ternary and assignment go right to left, which is why a = b = c = 7 works.
Precedence Isn't Evaluation Order
Precedence only decides how the expression is grouped. Java still evaluates operands from left to right:
public class LeftFirst {
public static void main(String[] args) {
int i = 2;
int result = i + i * ++i;
System.out.println("result = " + result);
}
}result = 8- Grouping makes it
i + (i * ++i), because*binds tighter than+. - Java still reads the operands left to right: the first
iis 2. - Inside the brackets, the second
iis also 2, then++imakesi3 and gives 3, soi * ++iis2 * 3 = 6. - Finally
2 + 6 = 8. A common wrong answer is 9 or 11, from assuming++iruns first because it has the highest precedence.
Add Brackets When in Doubt
Brackets cost nothing at runtime and remove all doubt:
| Hard to read | Clear |
|---|---|
a + b * c - d / e | a + (b * c) - (d / e) |
x > 0 && y > 0 || z > 0 | (x > 0 && y > 0) || z > 0 |
total * 18 / 100 + total | total + (total * 18 / 100) |
Common Mistakes
Mixing && and || without brackets. && binds tighter, so a || b && c means a || (b && c). Add brackets to show what you mean.
Forgetting that + with a String joins. "Sum: " + 2 + 3 is "Sum: 23"; "Sum: " + (2 + 3) is "Sum: 5".
Thinking higher precedence means "runs earlier in time". Operands are still read left to right; precedence only groups them.
Practice
- Easy: Evaluate by hand, then check:
20 - 4 * 2 + 6 / 3,(20 - 4) * (2 + 6) / 3,17 % 5 * 2. - Harder: Add brackets to
age >= 60 || isStudent && hasIdso that the concession needs an ID for both seniors and students. What does the original version mean?
Key Takeaways
Was this tutorial helpful?