⬅ Previous Topic
Java OperatorsNext Topic ⮕
Java Basic Input and Output⬅ Previous Topic
Java OperatorsNext Topic ⮕
Java Basic Input and OutputIn 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.
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.
When two operators have the same precedence, associativity determines the direction of evaluation. It can be either:
+
, -
, *
, /
)Here’s a simplified view of Java operators from highest to lowest precedence:
Precedence | Operators | Associativity |
---|---|---|
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 |
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.
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.
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
.
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
.
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.
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.
int result = 5 + 3 * 2;
System.out.println(result);
a = b = c = 10;
assigns the value 10 to all three variables using left-to-right associativity.
boolean result = false || true && false;
System.out.println(result);
⬅ Previous Topic
Java OperatorsNext Topic ⮕
Java Basic Input and OutputYou 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.