Home Java Operator Precedence & Associativity

Operator Precedence and Associativity in Java

Beginner ⏱ 9 min read Updated: Jun 2026

Operator precedence and associativity in Java define the order in which operators are evaluated in an expression. They help Java determine which operation should be performed first when multiple operators are used together.

Understanding these concepts is important because incorrect assumptions about evaluation order can lead to unexpected results in Java programs.

What is Operator Precedence in Java?

Operator precedence refers to the priority or ranking of operators. When an expression contains multiple operators, Java evaluates the operator with higher precedence first.

For example, multiplication has higher precedence than addition, so multiplication is performed before addition.

Example
int result =  10 + 5 *  2;
System.out.println(result);
Output
20

In the above example, multiplication (*) is performed first because it has higher precedence than addition (+).

What is Operator Associativity in Java?

Operator associativity defines the direction in which operators with the same precedence are evaluated.

When two or more operators have the same priority, Java uses associativity rules to decide whether the expression should be evaluated from left to right or right to left.

💡
Key Point: Precedence decides which operator executes first, while associativity decides the evaluation direction.

Difference Between Operator Precedence and Associativity

Operator Precedence Operator Associativity
Defines operator priority. Defines evaluation direction.
Determines which operator runs first. Determines order when priority is same.
Example: * executes before + Example: + evaluates left to right

Java Operator Precedence Table

The following table shows the common operator precedence order in Java. Operators at the top have higher priority than operators below them.

Priority Operators Associativity
Highest () [] . Left to Right
2 ++ -- ! ~ Right to Left
3 * / % Left to Right
4 + - Left to Right
5 << >> >>> Left to Right
6 < <= > >= instanceof Left to Right

More Java Operator Precedence Levels

Priority Operators Associativity
7 == != Left to Right
8 & Left to Right
9 ^ Left to Right
10 | Left to Right
11 && Left to Right
12 || Left to Right
13 ? : Right to Left
Lowest = += -= *= /= %= Right to Left

Example of Operator Precedence in Java

Java follows operator precedence rules to evaluate expressions. The operator with higher priority is executed before the lower priority operator.

Example
int result = 20 - 10 /  2;
System.out.println(result);
Output
15

Explanation:

  • Division (/) has higher precedence than subtraction (-).
  • First, 10 / 2 is calculated, which gives 5.
  • Then, 20 - 5 is calculated, giving the final result 15.

Using Parentheses to Change Precedence

Parentheses () have the highest priority in Java. They can be used to change the default order of evaluation.

Example
int  result = (10 +  5) * 2;
System.out.println(result);
Output
30

In this example, the addition operation is performed first because it is inside parentheses.

Example of Operator Associativity in Java

When multiple operators have the same precedence level, Java evaluates them according to associativity rules.

Left to Right Associativity Example

Example
int result = 20 - 5 + 10;
System.out.println(result);
Output
25

Explanation:

  • Addition and subtraction have the same precedence.
  • They follow left-to-right associativity.
  • First: 20 - 5 = 15
  • Then: 15 + 10 = 25

Right to Left Associativity Example

Assignment operators follow right-to-left associativity.

Example
int 
a, b, c;
a = b = c = 50;
System.out.println(a);
Output
50

Advantages of Understanding Operator Precedence and Associativity

Correct Evaluation

Helps understand how Java evaluates complex expressions and prevents unexpected results.

Better Code Quality

Allows developers to write clear and predictable Java programs.

Error Prevention

Reduces logical errors caused by incorrect assumptions about operator execution order.

Efficient Debugging

Makes it easier to find and fix problems in complex expressions.

Common Mistakes While Using Operator Precedence

  • Assuming all operators are evaluated from left to right.
  • Forgetting that multiplication and division have higher precedence than addition and subtraction.
  • Writing complex expressions without using parentheses.
  • Confusing assignment operators with equality operators.
  • Ignoring associativity rules for operators with the same precedence.

Tips for Writing Clear Java Expressions

  • Use parentheses to make expressions easier to understand.
  • Avoid writing very complex expressions in a single line.
  • Follow Java operator priority rules while debugging code.
  • Add meaningful variable names to improve readability.

Example Combining Multiple Operators in Java

Example
int  result = 10 +  5 *  2 >  15;
System.out.println(result);
Output
true

Explanation:

  • Multiplication (*) is performed first: 5 * 2 = 10.
  • Addition (+) is performed next: 10 + 10 = 20.
  • Comparison operator (>) checks: 20 > 15.
  • Final result is true.

Summary

Operator precedence and associativity in Java determine the order in which operators are evaluated inside an expression.

Operator precedence decides which operator gets executed first, while associativity decides the evaluation direction when operators have the same priority.

Understanding these concepts helps developers write accurate, readable, and error-free Java programs.