Operator Precedence
3 min read ·
Operator precedence defines the order in which operators are evaluated in an expression.
When multiple operators are used in a single expression, Java follows a specific priority rule to decide which operation runs first.
If operators have the same precedence, associativity decides the order of execution.
Operator Precedence Table
Higher precedence operators are evaluated first.
| Precedence Level | Operators | Description | Associativity |
|---|---|---|---|
| 1 | ++ -- ! | Unary operators | Right to Left |
| 2 | * / % | Multiplication Division Modulus | Left to Right |
| 3 | + - | Addition Subtraction | Left to Right |
| 4 | > < >= <= | Relational operators | Left to Right |
| 5 | == != | Equality operators | Left to Right |
| 6 | && | Logical AND | Left to Right |
| 7 | || | Logical OR | Left to Right |
| 8 | = += -= *= /= %= | Assignment operators | Right to Left |
Example 1 Basic Arithmetic
Output
20
Explanation
Multiplication runs before addition.
Example 2 Using Parentheses
Output
30
Parentheses have highest priority and force evaluation first.
Example 3 Comparison and Logical Operators
Explanation
First relational operators are evaluated.
Then logical AND is applied.
Example 4 Complex Expression
Step by step evaluation
3 * 2 = 6
4 / 2 = 2
5 + 6 - 2 = 9
Output
9
Example 5 Assignment Precedence
Multiplication runs first
Then addition
Then assignment happens at the end
Associativity Concept
When operators have same precedence, associativity decides order.
Example
Subtraction follows Left to Right rule
20 - 5 = 15
15 - 3 = 12
Output
12
Unary Operator Example
Unary operator runs first
Then multiplication
Output
12
Pro Tip
When expression becomes confusing, always use parentheses to make logic clear and readable.
Practice Exercise
- Evaluate expression 8 + 4 * 3 and predict output
- Write program using parentheses to change result
- Create expression using comparison and logical operators
- Test associativity using subtraction and division