Operator Precedence
Operator Precedence
A school program needs reliable data and clear rules. Precedence determines how operators group when parentheses are absent. Multiplication groups before addition, so the placement of parentheses can change a marks calculation.
Understand
Precedence determines how operators group when parentheses are absent.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
Precedence groups expressions; it is not a reason to rely on complicated side effects. Add parentheses when they clarify the intended calculation.
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:
- Left-to-right: Most binary operators (e.g.,
+,-,*,/) - Right-to-left: Unary, assignment, and ternary operators
Java Operator Precedence Table
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 |
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
- Use parentheses generously for clarity, not just correctness.
- Always know your associativity when chaining operators.
- Don’t rely solely on precedence—aim for readable, maintainable code.
Important boundaries and mistakes
Precedence groups expressions; it is not a reason to rely on complicated side effects. Add parentheses when they clarify the intended calculation.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Precedence groups expressions; it is not a reason to rely on complicated side effects.
Java 17 · Main.java · complete practice program
public class Main {
public static void main(String[] args) {
System.out.println(10 + 5 * 2);
System.out.println((10 + 5) * 2);
}
}
Now change one thing: Replace the initial 10 with 20. Predict the new behavior before checking the worked variation.
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
public static void main(String[] args) {
System.out.println(10 + 5 * 2);
System.out.println((10 + 5) * 2);
}
}
Expected output
20
30Precedence determines how operators group when parentheses are absent. Multiplication groups before addition, so the placement of parentheses can change a marks calculation.
Precedence groups expressions; it is not a reason to rely on complicated side effects. Add parentheses when they clarify the intended calculation.
Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.
Worked variation: Replace the initial 10 with 20. The two expressions produce 30 and 50, respectively.
Check your understanding
Check a common mistake
What to remember
Precedence determines how operators group when parentheses are absent. Multiplication groups before addition, so the placement of parentheses can change a marks calculation. Precedence groups expressions; it is not a reason to rely on complicated side effects. Add parentheses when they clarify the intended calculation.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.