Operator Precedence in Java
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.
Higher precedence runs first
In 2 + 3 * 4, multiplication's higher precedence means it evaluates before the addition, giving 14.
Parentheses override everything
(2 + 3) * 4 forces the addition to happen first regardless of the operators' normal precedence, giving 20.
Comparisons bind looser than arithmetic
a + b > c * d evaluates both arithmetic expressions completely before comparing their results.
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.
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.
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); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.