skip to content

Operator Precedence & Associativity

Precedence decides which operator binds first and associativity decides the direction, which is why assignment chains right to left. Interviewers rarely ask you to recite the table, but they will hand you an expression that depends on it.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

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

open as a page

What is operator associativity, and how does it differ from precedence? Give an example where assignment's associativity matters.

level: middleimportance: must knowfreq 55%

basics

~20 s

Associativity decides the grouping when operators have the same precedence. Most operators group left to right (a - b - c is (a - b) - c). Assignment groups right to left, so a = b = 0 sets b to 0 first, then a.

open as a page

Why does an expression like flags & MASK == 0 often behave unexpectedly in Java, and how does the precedence table explain it?

level: middleimportance: should knowfreq 40%

basics

~20 s

Because == has higher precedence than &, Java reads it as flags & (MASK == 0), not (flags & MASK) == 0. The comparison runs first, which is almost never what you meant. Add parentheses: (flags & MASK) == 0.

open as a page

How does operator precedence differ from operand evaluation order in Java? Use an expression with side effects to illustrate.

level: seniorimportance: should knowfreq 45%

basics

~20 s

Precedence decides how operators group their operands; evaluation order decides which operand runs first in time. In Java operands always run left to right, no matter the precedence, so a higher-precedence operator can still have its operands computed later.

open as a page

As a tech lead, how would you set a team policy around operator precedence to prevent subtle bugs, and where do you draw the line between relying on the table and using explicit parentheses?

level: principalimportance: nice to knowfreq 25%

basics

~20 s

Let obvious arithmetic precedence stand (a * b + c), but require parentheses whenever you mix different operator families — especially bitwise with comparison, or assignment/ternary in chains. Back it with linter rules so it's automatic, not memory.

open as a page