Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Operator Precedence



Operator Precedence in Java

In Java, when you write a complex expression involving multiple operators, the order in which those operators are evaluated is governed by a set of rules called operator precedence.

If you've ever wondered why 2 + 3 * 4 doesn't equal 20, then operator precedence is the reason.

What is Operator Precedence?

Operator precedence determines the order in which different operators in an expression are evaluated. For instance, multiplication has higher precedence than addition, so it is performed first.

What is Associativity?

When two operators have the same precedence, associativity determines the direction of evaluation. It can be either:

Java Operator Precedence Table

Here’s a simplified view of Java operators from highest to lowest precedence:

PrecedenceOperatorsAssociativity
1(), [], .Left to Right
2++, -- (postfix)Left to Right
3++, --, +, -, ! (unary)Right to Left
4*, /, %Left to Right
5+, -Left to Right
6<<, >>, >>>Left to Right
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||Left to Right
14?:Right to Left
15=, +=, -=, etc.Right to Left

Basic Example: Precedence in Action

public class Main {
  public static void main(String[] args) {
    int result = 10 + 2 * 3;
    System.out.println("Result: " + result);
  }
}
Result: 16

Explanation: 2 * 3 is evaluated first (due to higher precedence), and then 10 + 6 is evaluated.

Using Parentheses to Control Precedence

public class Main {
  public static void main(String[] args) {
    int result = (10 + 2) * 3;
    System.out.println("Result: " + result);
  }
}
Result: 36

Explanation: By wrapping 10 + 2 in parentheses, we change the natural order of evaluation.

Associativity Example

public class Main {
  public static void main(String[] args) {
    int a = 20, b = 10, c = 5;
    int result = a - b - c;
    System.out.println("Result: " + result);
  }
}
Result: 5

Explanation: Since - has left-to-right associativity, evaluation happens like (20 - 10) - 5.

Assignment Associativity (Right to Left)

public class Main {
  public static void main(String[] args) {
    int a, b, c;
    a = b = c = 100;
    System.out.println(a + " " + b + " " + c);
  }
}
100 100 100

Explanation: Assignment operators are evaluated from right to left: c = 100, then b = c, then a = b.

Common Pitfall Example

public class Main {
  public static void main(String[] args) {
    boolean result = true || false && false;
    System.out.println(result);
  }
}
true

Explanation: && has higher precedence than ||, so false && false is evaluated first (which is false), then true || false becomes true.

Why Precedence Matters

Even a minor mistake in understanding precedence can lead to incorrect logic and subtle bugs. Always use parentheses to make your intent clear, especially in complex conditions or calculations.

Best Practices

QUIZ

Question 1:What is the output of the following code?

int result = 5 + 3 * 2;
System.out.println(result);

Question 2:In Java, the expression a = b = c = 10; assigns the value 10 to all three variables using left-to-right associativity.

Question 3:Which operator group has the highest precedence in Java?

Question 4:Which of the following are true about operator associativity in Java?

Question 5:What will be printed by the following Java code?

boolean result = false || true && false;
System.out.println(result);

Question 6:Using parentheses in Java expressions can override operator precedence rules.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M