Java Operator Precedence and Associativity

 
When writing Java programs, you often use multiple operators in a single expression. For example:
 
int result = 10 + 5 * 2;
 
 
At first glance, you may wonder – should Java add 10 + 5 first or multiply 5 * 2 first?
 
The answer lies in operator precedence and associativity. These two concepts determine the order in which Java evaluates operators in complex expressions.
 

1. What is Operator Precedence?

 

Operator precedence defines the priority level of operators.

Operators with higher precedence are evaluated before operators with lower precedence.

For example, in Java, the multiplication operator * has higher precedence than addition +.

So, in this expression:

int result = 10 + 5 * 2;

Java first evaluates 5 * 2 = 10 and then performs 10 + 10 = 20.
Thus, the output is:

20

2. What is Operator Associativity?

 

When two operators have the same precedence, associativity decides the order of evaluation.

Most operators in Java are left-to-right associative (evaluated from left to right).

A few operators, like assignment (=) and ternary (?:), are right-to-left associative.

Example:

int x = 10, y = 20, z = 30;
int result = x + y + z;

Here, + has left-to-right associativity, so Java evaluates it as:

(x + y) + z = (10 + 20) + 30 = 60

3. Java Operator Precedence Table

 

Here’s a simplified version of the operator precedence table (from highest to lowest):

Precedence LevelOperator(s)AssociativityExample
1 (Highest)() (parentheses), [], .Left-to-right(a + b) * c
2++ -- (postfix)Left-to-righta++
3++ -- (prefix), + (unary), - (unary), !, ~Right-to-left++a, -b, !flag
4* / %Left-to-righta * b / c
5+ - (binary)Left-to-righta + b - c
6<< >> >>>Left-to-righta << 2
7< <= > >=Left-to-righta < b
8== !=Left-to-righta == b
9&Left-to-righta & b
10^Left-to-righta ^ b
11``Left-to-right
12&&Left-to-righta && b
13` `
14?: (ternary)Right-to-left(a > b) ? x : y
15 (Lowest)= += -= *= /= etc.Right-to-lefta = b + c

4. Examples of Operator Precedence and Associativity in Action

 

Example 1: Precedence


public class Test {
  public static void main(String[] args) {
    int result = 10 + 20 * 3;
    System.out.println(result);
  }
}

Explanation:

* has higher precedence than +.

First 20 * 3 = 60

Then 10 + 60 = 70

Output:

70

Example 2: Associativity


public class Test {
  public static void main(String[] args) {
    int a = 5, b = 10, c = 15;
    int result = a + b + c;
    System.out.println(result);
  }
}

Explanation:

+ has left-to-right associativity.

(5 + 10) + 15 = 30

Output:

30

Example 3: Right-to-left Associativity


public class Test {
  public static void main(String[] args) {
    int x = 5;
    int y = 10;
    int z = 15;

    x = y = z; // Assignment is right-to-left
    System.out.println(x);
    System.out.println(y);
    System.out.println(z);
  }
}

Explanation:

Assignment (=) is right-to-left.

First y = z → y = 15

Then x = y → x = 15

Output:

15
15
15

Example 4: Parentheses Change the Precedence


public class Test {
  public static void main(String[] args) {
    int result1 = 10 + 5 * 2; // Precedence applies
    int result2 = (10 + 5) * 2; // Parentheses override precedence
    System.out.println(result1);
    System.out.println(result2);
  }
}

Output:

20
30

5. Common Pitfalls

 

Mistaking associativity direction

int result = 100 / 10 * 2; // Correct: (100 / 10) * 2 = 20

Misunderstanding ternary operator associativity

int a = 5, b = 10, c = 15;
int result = a > b ? a : b > c ? b : c;
// This is evaluated as: a > b ? a : (b > c ? b : c)

6. Best Practices

 
  1. Use parentheses liberally to make code more readable and avoid ambiguity.
  2. Don’t rely solely on precedence rules for complex expressions—clarity is more important than cleverness.
  3. Know your associativity, especially with assignment and conditional operators.

7. Summary

ConceptMeaning
PrecedenceDetermines which operator is evaluated first
AssociativityDetermines direction of evaluation when precedence is the same
ParenthesesOverride both precedence and associativity
Java Operator Precedence and Associativity

Here’s a visual precedence chart showing Java operators from highest (top) to lowest (bottom) precedence.

Java Evaluation Tree

Here’s a step-by-step evaluation tree for the expression 10 + 5 * 2:

Java first evaluates 5 * 2 = 10 (because * has higher precedence).

Then it evaluates 10 + 10 = 20.

Here’s the evaluation tree for (10 + 5) * 2:

Parentheses force Java to evaluate 10 + 5 = 15 first.

Then it multiplies 15 * 2 = 30.

Comparing this with the earlier tree for 10 + 5 * 2 makes it very clear how parentheses override operator precedence.

Conclusion

 

Operator precedence and associativity are foundational to understanding how Java evaluates expressions. Mastering them helps you write more predictable, bug-free code. When in doubt, use parentheses to ensure your logic is crystal clear—for both you and anyone else reading your code.

Scroll to Top