Java Operator Precedence
Java Operator Precedence
Highest to Lowest Precedence Table
| Precedence Level | Operators | Type | Associativity |
|---|---|---|---|
| 1️⃣ Highest | (), [], . |
Parentheses, array access, object access | Left → Right |
| 2️⃣ | ++, -- |
Unary (postfix) | Left → Right |
| 3️⃣ | ++, --, +, -, !, ~ |
Unary (prefix) | Right → Left |
| 4️⃣ | *, /, % |
Multiplicative | Left → Right |
| 5️⃣ | +, - |
Additive | Left → Right |
| 6️⃣ | <<, >>, >>> |
Shift operators | Left → Right |
| 7️⃣ | <, <=, >, >=, instanceof |
Comparison | Left → Right |
| 8️⃣ | ==, != |
Equality | Left → Right |
| 9️⃣ | & |
Bitwise AND | Left → Right |
| 🔟 | ^ |
Bitwise XOR | Left → Right |
| 1️⃣1️⃣ | |
Bitwise OR | |
| 1️⃣2️⃣ | && |
Logical AND | Left → Right |
| 1️⃣3️⃣ | |
||
| 1️⃣4️⃣ | ?: |
Ternary (conditional) | Right → Left |
| 1️⃣5️⃣ Lowest | =, +=, -=, *=, /=, etc. |
Assignment | Right → Left |
📌 Example 1: Without Parentheses
Output:
Why? * has higher precedence than +, so calculation is:
📌 Example 2: With Parentheses
📌 Example 3: Logical + Comparison
-
5 + 3→8 -
8 > 6→true -
2 < 5→true -
true && true→true
Output:
📌 Example 4: Assignment Right-to-Left
📌 Example 5: Ternary vs Arithmetic
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.
