Java Operator Precedence

Java Operator Precedence

Highest to Lowest Precedence Table

Precedence LevelOperatorsTypeAssociativity
1️⃣ Highest(), [], .Parentheses, array access, object accessLeft → Right
2️⃣++, --Unary (postfix)Left → Right
3️⃣++, --, +, -, !, ~Unary (prefix)Right → Left
4️⃣*, /, %MultiplicativeLeft → Right
5️⃣+, -AdditiveLeft → Right
6️⃣<<, >>, >>>Shift operatorsLeft → Right
7️⃣<, <=, >, >=, instanceofComparisonLeft → Right
8️⃣==, !=EqualityLeft → Right
9️⃣&Bitwise ANDLeft → Right
🔟^Bitwise XORLeft → Right
1️⃣1️⃣``Bitwise OR
1️⃣2️⃣&&Logical ANDLeft → Right
1️⃣3️⃣``
1️⃣4️⃣?:Ternary (conditional)Right → Left
1️⃣5️⃣ Lowest=, +=, -=, *=, /=, etc.AssignmentRight → Left

📌 Example 1: Without Parentheses

 

Output:

20

Why? * has higher precedence than +, so calculation is:

5 * 2 = 10
10 + 10 = 20

📌 Example 2: With Parentheses



Output:

30

📌 Example 3: Logical + Comparison


Breakdown:

  • 5 + 38

  • 8 > 6true

  • 2 < 5true

  • true && truetrue

Output:

true

📌 Example 4: Assignment Right-to-Left


Output:

10 10 10

📌 Example 5: Ternary vs Arithmetic



Output:

25

Because y + 5 is evaluated, since condition x > y is false.


✔️ Summary

  • Operators with higher precedence execute first.

  • Parentheses () can be used to control the order.

  • Assignment and ternary operators evaluate right-to-left.

You may also like...