Learn Java
import com.learnjava.operators.OpPrecedence;

Operator Precedence in Java

@Concept

When an expression mixes several operators, precedence decides which one runs first — multiplication and division bind tighter than addition and subtraction, so 2 + 3 * 4 evaluates the multiplication first, giving 14, not 20. Every operator in Java has a documented precedence level, from highest (like postfix ++ and method calls) down to lowest (like the assignment operators), and parentheses always override precedence, forcing whatever they wrap to evaluate first regardless of what surrounds it.

Precedence resolves which operator binds tighter; associativity resolves ties between operators at the same precedence level, deciding whether an expression groups from the left or the right. Most operators, like - and /, associate left-to-right, but assignment and the unary operators associate right-to-left — which is why a = b = 5 assigns 5 to b first, and then that same result to a.

@HowItWorks
1

Higher precedence runs first

In 2 + 3 * 4, multiplication's higher precedence means it evaluates before the addition, giving 14.

2

Parentheses override everything

(2 + 3) * 4 forces the addition to happen first regardless of the operators' normal precedence, giving 20.

3

Comparisons bind looser than arithmetic

a + b > c * d evaluates both arithmetic expressions completely before comparing their results.

4

Logical operators bind looser still

&& binds tighter than ||, so a || b && c evaluates the && portion first — the same idea as multiplication binding tighter than addition.

5

Associativity breaks ties

Operators at the same precedence level group left-to-right in most cases (10 - 3 - 2 is (10 - 3) - 2, not 10 - (3 - 2)), but assignment groups right-to-left instead.

@Example
public class PrecedenceDemo {
public static void main(String[] args) {
int result = 2 + 3 * 4;
int forced = (2 + 3) * 4;
boolean check = 5 > 2 && 3 > 1;
System.out.println("Without parens: " + result);
System.out.println("With parens: " + forced);
System.out.println("Combined logic: " + check);
}
}
Output
Without parens: 14
With parens: 20
Combined logic: true
12 + 3 * 4 evaluates the multiplication first because it has higher precedence, giving 12, then adds 2 for a final 14.
2(2 + 3) * 4 forces the addition to run first because of the parentheses, giving 5, then multiplies by 4 for 20.
35 > 2 && 3 > 1 evaluates both comparisons first, since each has higher precedence than &&, then combines their two true results.
4All three println calls run once each, showing precedence visibly changing the answer.
@UseCases
Debugging a calculation that gives a surprising number until parentheses are added
Reading a dense conditional and knowing which comparisons group together before the && or ||
Deciding when to add parentheses purely for a future reader's clarity, even where they're not strictly required
Understanding why a = b = 5 assigns to both variables in one line

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.