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 LevelOperatorsDescriptionAssociativity
1++ -- !Unary operatorsRight to Left
2* / %Multiplication Division ModulusLeft to Right
3+ -Addition SubtractionLeft to Right
4> < >= <=Relational operatorsLeft to Right
5== !=Equality operatorsLeft to Right
6&&Logical ANDLeft to Right
7||Logical ORLeft to Right
8= += -= *= /= %=Assignment operatorsRight 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

  1. Evaluate expression 8 + 4 * 3 and predict output
  2. Write program using parentheses to change result
  3. Create expression using comparison and logical operators
  4. Test associativity using subtraction and division