Operator Precedence
Operator precedence defines the order in which operators are evaluated in an expression. When multiple operators appear in a single statement, Java follows a fixed priority hierarchy to decide which operation executes first. This topic is critical for avoiding logical bugs and is a frequent interview favorite.
Why Operator Precedence Matters
- Prevents ambiguous expression evaluation
- Avoids unexpected results
- Essential for writing correct conditions and calculations
- Helps understand complex expressions in legacy code
Basic Rule
Higher-precedence operators are evaluated before lower-precedence operators. If operators have the same precedence, Java uses associativity (left-to-right or right-to-left).
Operator Precedence Table (High → Low)
| Precedence | Operators | Description | Associativity |
|---|---|---|---|
| 1 | (), [], . | Parentheses, array access, member access | Left to Right |
| 2 | ++, --, +, -, !, ~ | Unary operators | Right to Left |
| 3 | *, /, % | Multiplication, Division, Modulus | Left to Right |
| 4 | +, - | Addition, Subtraction | Left to Right |
| 5 | <<, >>, >>> | Shift operators | Left to Right |
| 6 | <, <=, >, >=, instanceof | Relational | 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 operator | Right to Left |
| 14 | =, +=, -=, *=, /=, %= | Assignment | Right to Left |
Common Precedence Examples
Arithmetic Precedence
int result = 10 + 5 * 2;
System.out.println(result); // 20
Why: * has higher precedence than +.
Using Parentheses to Change Order
int result = (10 + 5) * 2;
System.out.println(result); // 30
Relational vs Logical Precedence
int a = 10, b = 20, c = 30;
boolean result = a < b && b < c;
Evaluation Order:
- a < b
- b < c
- &&
Assignment Precedence (Right to Left)
int a, b, c;
a = b = c = 5;
Why: Assignment operators are evaluated right to left.
Ternary Operator Precedence
int x = 10;
int y = 20;
int max = (x > y) ? x : y;
Important: Ternary has lower precedence than most operators, but higher than assignment.
Unary Operator Precedence
int a = 5;
int b = ++a * 2;
Evaluation:
- ++a → 6
- 6 * 2 → 12
Tricky Interview Example
System.out.println(10 + 20 + "Java" + 30 + 40);
Evaluation:
- 10 + 20 → 30
- "30" + "Java" → "30Java"
- "30Java" + 30 + 40 → "30Java3040"
Best Practices (Real-Time Coding)
- Use parentheses for clarity
- Avoid overly complex expressions
- Never rely on precedence for readability
- Prefer clean, explicit logic
Common Beginner Mistakes
- Assuming left-to-right evaluation always applies
- Forgetting precedence between && and ||
- Writing unreadable chained expressions
- Ignoring parentheses in ternary logic
Interview-Ready Answers
Short Answer
Operator precedence determines the order in which operators are evaluated in an expression.
Detailed Answer
Java follows a predefined operator precedence hierarchy to evaluate expressions. Operators with higher precedence are executed before lower-precedence ones, and when precedence is equal, associativity rules determine the evaluation order.
Key Takeaway
Operator precedence ensures predictable expression evaluation. Always use parentheses for clarity, especially in conditions and calculations.