Operator precedence and associativity are important concepts in Java that determine the order in which operators are evaluated in an expression. When multiple operators are used together, precedence decides which operator is evaluated first, while associativity determines the evaluation order when operators have the same precedence.
- Operator precedence defines the priority of operators in an expression.
- Operator associativity determines the evaluation order of operators with the same precedence.
- Both rules work together to produce the final result of an expression.
Operator Precedence
Operator precedence specifies the order in which different operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence, regardless of their position in the expression.
- Higher-precedence operators are evaluated before lower-precedence operators.
- Parentheses () can be used to override the default precedence.
- Helps avoid ambiguity in complex expressions
Syntax:
operand1 operator operand2
For expression:
10+20*30
The expression contains two operators, + (addition) and * (multiplication). According to operator precedence, multiplication (*) has higher precedence than addition (+), so multiplication is checked first. After evaluating multiplication, the addition operator is then evaluated to give the final result.

Example:
public class OperatorPrecedenceExample {
public static void main(String[] args)
{
// Multiplication has higher precedence than
// addition
int result = 10 + 20 * 30;
System.out.println("Result: " + result);
}
}
Output
Result: 610
Explanation:
- In the following code (multiplication) has higher precedence than + (addition).
- So in 10 + 20 * 30, the multiplication happens first:
20 * 30 = 600 // first execute this expression
10 + 600 = 610// next execute this expression
Operator Precedence Table
The operator precedence table defines the priority levels of Java operators. Operators at the top of the table have higher precedence, while operators at the bottom have lower precedence.
- Parentheses () have the highest precedence.
- Arithmetic operators generally have higher precedence than relational and logical operators.
- Assignment operators have lower precedence than most other operators.

Operator Associativity
Operator associativity determines the order in which operators of the same precedence are evaluated. Java supports both left-to-right and right-to-left associativity depending on the operator type.
- Applied only when operators have the same precedence.
- Java supports left-to-right and right-to-left associativity.
Two types of associativity
1. Left-to-Right Associativity
When multiple operators of the same precedence appear in an expression, they are evaluated from left to right. For example, in the expression a + b - c, addition and subtraction have the same precedence and are left-associative, so the expression is evaluated as (a + b) - c.
Operators with left-to-right associativity include:
- Arithmetic operators: +, -, *, /, %
- Relational operators: >, <, >=, <=
- Logical AND/OR: &&, ||
- Bitwise operators: &, |, ^, <<, >>
public class LeftToRight {
public static void main(String[] args)
{
int a = 10, b = 5, c = 2;
int result
= a - b + c; // evaluated as (10 - 5) + 2 = 7
System.out.println("Final output " + result);
}
}
Output
Final output 7
Explanation: Here, - and + have the same precedence and are left-associative, so evaluation happens left to right.
2. Right-to-Left Associativity
Right-to-Left Associativity means that operators are evaluated from right to left when they have the same precedence.
Operators with right-to-left associativity include:
- Assignment operators: =, +=, -=, etc.
- Unary operators: ++, --, !, ~
public class RightToLeftAssociativity {
public static void main(String[] args)
{
int a, b;
a = b = 4; // evaluated as a = (b = 4)
System.out.println("a: " + a); // 4
System.out.println("b: " + b); // 4
}
}
Output
a: 4 b: 4
Explanation: The assignment = is right-associative, so b = 4 is evaluated first, then a = b.
For expression:
100 / 10 % 10
The division (/) and modulus (%) operators have the same precedence, so the order in which they are evaluated depends on their left-to-right associativity. This means the division is performed first, followed by the modulus operation. After the calculations, the result of the modulus operation is determined.

Example of Java Operator Precedence and Associativity
In the expression 100 + 200 / 10 - 3 * 10, division (/) and multiplication (*) are evaluated first because they have higher precedence than addition (+) and subtraction (-). The remaining operations are then evaluated from left to right, resulting in the final value 90.

Example: Now see how this expression evaluate:
int exp = 100 + 200 / 10 - 3 * 10;
public class OperatorPrecedence {
public static void main(String[] args)
{
int result = 100 + 200 / 10 - 3 * 10;
// Verifying the result of the same expression
System.out.println("Final Output: " + result);
}
}
Output
Final Output: 90
Explanation:
- * and / have higher precedence than + and -
200 / 10 = 20
3 * 10 = 30 // Evaluate left to right:
- + and - have same precedence, evaluated left to right:
100 + 20 = 120
120 - 30 = 90 // Evaluate left to right