Operator Precedence and Associativity

Naveen AnamaneniUpdated · 6 min read

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

Evaluation order of 2 + 3 * 4 - 6 / 2
Order.java▶ Try it
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);
    }
}
Output
11 20 true

How it works

  1. Line 3: * and / come before + and -. So 3 * 4 = 12 and 6 / 2 = 3 first, then 2 + 12 - 3 = 11.
  2. Line 4: brackets come first, so (2 + 3) = 5, then 5 * 4 = 20.
  3. Line 5: arithmetic, then comparison, then &&. 5 + 3 > 7 is 8 > 7, which is true; 10 % 3 == 1 is 1 == 1, which is true; true && true is true.

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:

LevelOperatorsAssociativity
1Postfix: x++ x--Left to right
2Unary: ++x --x + - ! ~ (type)Right to left
3Multiplicative: * / %Left to right
4Additive: + -Left to right
5Shift: << >> >>>Left to right
6Relational: < > <= >= instanceofLeft to right
7Equality: == !=Left to right
8Bitwise AND: &Left to right
9Bitwise XOR: ^Left to right
10Bitwise OR: |Left to right
11Logical AND: &&Left to right
12Logical OR: ||Left to right
13Ternary: ? :Right to left
14Assignment: = += -= and friendsRight to left

Associativity: Same Level, Which Way?

When operators of the same level meet, associativity decides:

ExpressionAssociativityStepsResult
10 - 4 - 3Left to right(10 - 4) - 33
100 / 10 / 5Left to right(100 / 10) / 52
a = b = c = 7Right to lefta = (b = (c = 7))all three are 7
!!trueRight 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:

LeftFirst.java▶ Try it
public class LeftFirst {
    public static void main(String[] args) {
        int i = 2;
        int result = i + i * ++i;
        System.out.println("result = " + result);
    }
}
Output
result = 8
  1. Grouping makes it i + (i * ++i), because * binds tighter than +.
  2. Java still reads the operands left to right: the first i is 2.
  3. Inside the brackets, the second i is also 2, then ++i makes i 3 and gives 3, so i * ++i is 2 * 3 = 6.
  4. Finally 2 + 6 = 8. A common wrong answer is 9 or 11, from assuming ++i runs first because it has the highest precedence.

Add Brackets When in Doubt

Brackets cost nothing at runtime and remove all doubt:

Hard to readClear
a + b * c - d / ea + (b * c) - (d / e)
x > 0 && y > 0 || z > 0(x > 0 && y > 0) || z > 0
total * 18 / 100 + totaltotal + (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

  1. Easy: Evaluate by hand, then check: 20 - 4 * 2 + 6 / 3, (20 - 4) * (2 + 6) / 3, 17 % 5 * 2.
  2. Harder: Add brackets to age >= 60 || isStudent && hasId so that the concession needs an ID for both seniors and students. What does the original version mean?

Quick check: Precedence

4 questions
1What is 2 + 3 * 4?
2What is 10 - 4 - 3?
3Which runs first in a || b && c?
4Why does a = b = c = 7; set all three to 7?

Key Takeaways

Was this tutorial helpful?

Lesson 46 of 482