skip to content

What is operator precedence in Java, and why does it matter when you write expressions without parentheses?

level: juniorimportance: must knowfreq 60%

answer

  1. Ranking of operators decides grouping
  2. before + : 2 + 3 * 4 = 14
  3. Grouping, not evaluation timing
  4. Parentheses always override
  5. Prefer parentheses for clarity

basics

~20 s

Operator precedence is the set of rules that decides which operators run first in an expression that has no parentheses. For example, * runs before +, so 2 + 3 * 4 is 14, not 20.

solid answer

~40 s

Operator precedence is the ranking that tells the Java compiler which operator binds its operands first when an expression contains several operators and no parentheses to force an order. Higher-precedence operators are grouped before lower-precedence ones: multiplicative (* / %) above additive (+ -), arithmetic above relational, relational above logical, and so on. So 2 + 3 * 4 is parsed as 2 + (3 * 4) = 14 because * outranks +. Precedence only decides grouping, not the order in which side effects happen — Java still evaluates operands strictly left to right. When the default grouping is not what you want, you add explicit parentheses, which always win. In practice, prefer parentheses for any non-obvious mix of operators so readers don't have to recall the table.

go deeper

for a junior

Knows the headline rule (multiplication before addition) and can evaluate simple mixed arithmetic correctly.

for a middle

Recalls the broader ranking (arithmetic > shift > relational > equality > logical) and that parentheses override it.

for a senior

Distinguishes grouping from evaluation order, knows operands evaluate left to right, and advocates parentheses for readability over relying on the table.

for a principal

Frames precedence as a source of subtle bugs in shared code, sets team conventions (mandatory parens for mixed bitwise/logical), and can reason about how it interacts with side effects and short-circuiting.

## What an operator and an expression are An **operator** is a symbol that performs a computation on one or more values, called its **operands**. `+`, `*`, `<`, `&&`, and `=` are all operators. An **expression** is any combination of values, variables, and operators that produces a value, like `2 + 3 * 4`. ## The problem precedence solves When an expression has more than one operator and you do not use parentheses, the compiler has to decide which operator 'grabs' its operands first. `2 + 3 * 4` could mean `(2 + 3) * 4 = 20` or `2 + (3 * 4) = 14`. These give different answers, so the language needs a fixed rule. That rule is **operator precedence**: every operator has a rank, and operators with a higher rank are applied (grouped) before operators with a lower rank. ## How Java ranks the common operators From higher to lower (a useful subset): 1. Postfix `expr++` `expr--` 2. Unary `++expr` `--expr` `+expr` `-expr` `!` `~` 3. Multiplicative `*` `/` `%` 4. Additive `+` `-` 5. Shift `<<` `>>` `>>>` 6. Relational `<` `<=` `>` `>=` `instanceof` 7. Equality `==` `!=` 8. Bitwise AND `&` 9. Bitwise XOR `^` 10. Bitwise OR `|` 11. Logical AND `&&` 12. Logical OR `||` 13. Ternary `? :` 14. Assignment `=` `+=` `-=` `*=` ... Because `*` (rank 3) outranks `+` (rank 4), `2 + 3 * 4` is grouped as `2 + (3 * 4) = 14`. ## Precedence is about grouping, not timing A crucial point that trips people up: precedence decides **how operands are grouped**, not the **order in which the operands are actually evaluated**. Java separately guarantees that operands are evaluated **strictly left to right**. So in `a() + b() * c()`, the calls happen in the order `a()`, `b()`, `c()` (left to right), but the multiplication of `b()*c()` is *grouped* first before being added to `a()`'s result. ## Parentheses always win Parentheses are not really an operator; they explicitly override grouping. `(2 + 3) * 4` forces the addition first, giving 20. Whenever the default grouping is unclear to a reader, add parentheses — they cost nothing at runtime and remove all ambiguity. ## Deriving an answer at any level - A junior should be able to say 'multiplication before addition' and reach 14 for `2 + 3 * 4`. - A mid-level engineer should know the broader ranking (arithmetic > relational > logical) and that parentheses override it. - A senior should add that precedence is grouping, evaluation order is separate (left-to-right), and that readability beats relying on the table.

  • Does higher precedence mean the operator's operands are evaluated first in time?
    No. Precedence only decides grouping. Operand evaluation in Java is strictly left to right regardless of precedence, so a higher-precedence operator can still have its operands evaluated after something to its left.
  • How would you remove all ambiguity from a confusing expression?
    Add explicit parentheses around the sub-expressions to force the intended grouping; parentheses always take priority over the precedence table and make intent obvious to readers.

saying these in an interview costs you the question

  • Saying precedence controls the order operands are evaluated (that is left-to-right, separate from precedence)
  • Claiming 2 + 3 * 4 equals 20
  • Thinking parentheses are just style and have no effect on the result

context