DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Java Operator Precedence

When an expression contains multiple operators, how does Java decide which operation to perform first?

The answer lies in operator precedence and associativity.

These concepts are frequently tested in Java interviews because they help determine how complex expressions are evaluated.

In this guide, you'll learn:

  • What operator precedence is
  • Complete precedence table
  • Associativity rules
  • Operand evaluation order
  • Parentheses and precedence
  • Common interview questions and tricky examples

What is Operator Precedence?

Operator precedence determines which operator is evaluated first when an expression contains multiple operators.

Higher-precedence operators are evaluated before lower-precedence operators unless parentheses change the order.


Complete Java Operator Precedence Table

Level Operator Type Operators Associativity Notes / Edge Cases
1 Unary / Creation ++, --, ~, !, new, cast Right-to-Left Prefix ++/-- and casts evaluate right-to-left.
2 Multiplicative *, /, % Left-to-Right Higher priority than addition/subtraction.
3 Additive +, - Left-to-Right Controls basic math and string concatenation.
4 Shift <<, >>, >>> Left-to-Right >>> is the unsigned (zero-fill) right shift.
5 Relational / Comparison <, <=, >, >=, instanceof Left-to-Right Used for structural bounds checking.
6 Equality ==, != Left-to-Right Compares primitives by value, objects by memory address.
7 Bitwise AND & Left-to-Right Evaluates both sides completely (no short-circuit).
8 Bitwise XOR ^ Left-to-Right Strict exclusive OR.
9 Bitwise OR | Left-to-Right The | requires a backslash escape inside markdown tables.
10 Short-circuit AND && Left-to-Right Skips RHS if LHS evaluates to false.
11 Short-circuit OR || Left-to-Right Skips RHS if LHS evaluates to true.
12 Ternary / Conditional ? : Right-to-Left The only three-operand operator in Java.
13 Assignment =, +=, -=, *=, etc. Right-to-Left Always happens dead last in an expression.

Tip: Many simplified charts combine multiplicative and additive operators into one "Arithmetic" category, but Java's actual precedence gives *, /, % higher precedence than + and -.


Arithmetic Precedence

Within arithmetic operators:

Higher precedence

*   /   %
Enter fullscreen mode Exit fullscreen mode

Lower precedence

+   -
Enter fullscreen mode Exit fullscreen mode

Example

int x = 2 + 3 * 4;
Enter fullscreen mode Exit fullscreen mode

Evaluation

3 * 4 = 12

↓

2 + 12

↓

14
Enter fullscreen mode Exit fullscreen mode

Output

14
Enter fullscreen mode Exit fullscreen mode

Another Example

int x = 10 + 20 / 5 - 2 * 3;
Enter fullscreen mode Exit fullscreen mode

Evaluation

20 / 5 = 4

2 * 3 = 6

10 + 4 - 6

↓

8
Enter fullscreen mode Exit fullscreen mode

Output

8
Enter fullscreen mode Exit fullscreen mode

Mixed Operators

Arithmetic Before Comparison

int x = 10;

boolean result = x + 5 > 12;
Enter fullscreen mode Exit fullscreen mode

Evaluation

10 + 5 = 15

↓

15 > 12

↓

true
Enter fullscreen mode Exit fullscreen mode

Comparison Before Logical

boolean result = 5 > 3 && 7 < 10;
Enter fullscreen mode Exit fullscreen mode

Evaluation

5 > 3

↓

true

7 < 10

↓

true

true && true

↓

true
Enter fullscreen mode Exit fullscreen mode

Unary Operators Have Higher Precedence

int x = 5;

int y = -x + 3;
Enter fullscreen mode Exit fullscreen mode

Evaluation

-x

↓

-5

↓

-5 + 3

↓

-2
Enter fullscreen mode Exit fullscreen mode

Output

-2
Enter fullscreen mode Exit fullscreen mode

Operator Precedence vs Operand Evaluation

These are different concepts.

Many beginners confuse them.

Operator Precedence

Determines which operator executes first.

Operand Evaluation

Determines which operand is evaluated first.


Java Rule

Operands are always evaluated from left to right.

Only after all operands are evaluated does Java apply operator precedence.


Famous Interview Example

public class Test {

    public static void main(String[] args) {

        System.out.println(
                m1(1)
                + m1(2) * m1(3) / m1(4) * m1(5)
                + m1(6)
        );
    }

    static int m1(int i) {
        System.out.println(i);
        return i;
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

1
2
3
4
5
6
12
Enter fullscreen mode Exit fullscreen mode

Notice:

The numbers 1 through 6 are printed in order.

This proves Java evaluates operands left to right.


Step 1: Operand Evaluation

Java first evaluates

m1(1)

↓

m1(2)

↓

m1(3)

↓

m1(4)

↓

m1(5)

↓

m1(6)
Enter fullscreen mode Exit fullscreen mode

Each method call prints its number.


Step 2: Apply Operator Precedence

The expression becomes

1 + 2 * 3 / 4 * 5 + 6
Enter fullscreen mode Exit fullscreen mode

Now apply precedence.

2 * 3

↓

6

6 / 4

↓

1

1 * 5

↓

5

1 + 5 + 6

↓

12
Enter fullscreen mode Exit fullscreen mode

Final output

12
Enter fullscreen mode Exit fullscreen mode

Operands First, Operators Later

Think of Java as doing two phases.

Phase 1

Evaluate operands

↓

Left to Right
Enter fullscreen mode Exit fullscreen mode

Phase 2

Apply operators

↓

According to precedence
Enter fullscreen mode Exit fullscreen mode

Associativity

What if two operators have the same precedence?

Associativity determines the order.


Left-to-Right Associativity

Most Java operators are left associative.

Example

20 / 5 * 2
Enter fullscreen mode Exit fullscreen mode

Evaluation

20 / 5

↓

4

4 * 2

↓

8
Enter fullscreen mode Exit fullscreen mode

Right-to-Left Associativity

Assignment operators are right associative.

Example

int a;
int b;
int c;

a = b = c = 10;
Enter fullscreen mode Exit fullscreen mode

Equivalent to

a = (b = (c = 10));
Enter fullscreen mode Exit fullscreen mode

Evaluation

c = 10

↓

b = 10

↓

a = 10
Enter fullscreen mode Exit fullscreen mode

Conditional Operator Associativity

The conditional operator is also right associative.

Example

condition1
? value1
: condition2
    ? value2
    : value3;
Enter fullscreen mode Exit fullscreen mode

This is interpreted as

condition1

?

value1

:

(condition2 ? value2 : value3)
Enter fullscreen mode Exit fullscreen mode

Parentheses Override Precedence

Parentheses always execute first.

Without parentheses

int x = 2 + 3 * 4;
Enter fullscreen mode Exit fullscreen mode

Output

14
Enter fullscreen mode Exit fullscreen mode

With parentheses

int y = (2 + 3) * 4;
Enter fullscreen mode Exit fullscreen mode

Evaluation

2 + 3

↓

5

5 * 4

↓

20
Enter fullscreen mode Exit fullscreen mode

Output

20
Enter fullscreen mode Exit fullscreen mode

Practical Examples

Example 1

10 + 5 == 15
Enter fullscreen mode Exit fullscreen mode

Evaluation

10 + 5

↓

15

15 == 15

↓

true
Enter fullscreen mode Exit fullscreen mode

Example 2

5 > 3 && 2 < 1
Enter fullscreen mode Exit fullscreen mode

Evaluation

true && false

↓

false
Enter fullscreen mode Exit fullscreen mode

Example 3

int a;
int b;

a = b = 5;
Enter fullscreen mode Exit fullscreen mode

Result

a = 5

b = 5
Enter fullscreen mode Exit fullscreen mode

A Note About Expressions Like ++x + x++

Expressions that modify the same variable multiple times in one statement can be confusing.

Although Java defines a left-to-right operand evaluation order, such expressions reduce readability and are discouraged in production code.

For interviews, evaluate them carefully by applying Java's operand evaluation order and the semantics of pre/post increment.


Interview Questions

What is operator precedence?

It determines which operator is evaluated first in an expression.


Which operators have the highest precedence?

Postfix and unary operators.


Which operators have the lowest precedence?

Assignment operators.


What evaluates first: operands or operators?

Operands.

Java evaluates operands from left to right before applying operator precedence.


What decides the order when two operators have the same precedence?

Associativity.


Which operators are right associative?

  • Assignment operators
  • Conditional (?:)
  • Prefix unary operators

How do you override precedence?

Using parentheses.


Memory Tricks 🧠

Easy Rule

Operands

↓

Left to Right

Operators

↓

By Precedence
Enter fullscreen mode Exit fullscreen mode

Highest to Lowest

PRECEDENCE (high to low):
1. () [] . ++ -- (postfix)
2. ++ -- + - ~ ! (prefix/unary)
3. * / %
4. + -
5. << >> >>>
6. < > <= >= instanceof
7. == !=
8. &
9. ^
10. |
11. &&
12. ||
13. ? :
14. = += -= *= /= %= etc.
Enter fullscreen mode Exit fullscreen mode

Remember

Parentheses beat everything.


Key Takeaways

  • Operator precedence determines the order in which operators are evaluated in an expression.
  • Java first evaluates operands from left to right, then applies operators according to precedence.
  • Multiplication, division, and modulo have higher precedence than addition and subtraction.
  • When operators have the same precedence, associativity determines the evaluation order.
  • Most operators are left-associative, while assignment (=) and the conditional operator (?:) are right-associative.
  • Parentheses always override the default precedence rules and improve code readability.
  • Understanding precedence and associativity helps avoid bugs and is a common Java interview topic.

If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Top comments (0)