skip to content

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

level: middleimportance: must knowfreq 55%

answer

  1. Associativity breaks same-precedence ties
  2. Almost all operators are left-associative
  3. Assignment and ternary are right-associative
  4. a = b = 0 -> a = (b = 0)
  5. 10/2/5 = (10/2)/5 = 1

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.

solid answer

~50 s

Precedence ranks operators of different priority; associativity breaks ties between operators of the *same* precedence by deciding whether grouping runs left-to-right or right-to-left. In Java, almost everything is left-associative: `a - b - c` means `(a - b) - c`, and `10 / 2 / 5` means `(10 / 2) / 5 = 1`, not `10 / (2 / 5)`. The notable right-associative operators are the assignment family (`=`, `+=`, etc.) and the ternary `? :`. Right-associativity is why chained assignment works: `a = b = 0` parses as `a = (b = 0)` — the inner `b = 0` runs, evaluates to 0, and that 0 is assigned to `a`. If assignment were left-associative, `a = b` would be evaluated first and you couldn't chain. Associativity, like precedence, governs grouping, not evaluation timing, which remains left to right.

go deeper

for a junior

Can state that subtraction goes left to right and chained assignment sets all variables to the same value.

for a middle

Defines associativity as the same-precedence tie-breaker, names assignment and ternary as right-associative, and works through a = b = 0 and 10/2/5 correctly.

for a senior

Explains why assignment must be right-associative (assignment-as-expression), and that associativity is grouping not evaluation order.

for a principal

Discusses how relying on associativity in chained ternaries or assignments hurts readability and sets conventions to avoid clever chaining in shared code.

## Why a tie-breaker is needed **Precedence** ranks operators of *different* priorities. But what about two operators that have the *same* precedence, like the two `-` signs in `a - b - c`? Precedence cannot break that tie because both operators sit at the same rank. **Associativity** is the rule that resolves it: it says whether equal-precedence operators group from the **left** or from the **right**. ## Left-associative (the common case) Most Java operators are **left-associative**, meaning the leftmost pair groups first: - `a - b - c` -> `(a - b) - c` - `10 / 2 / 5` -> `(10 / 2) / 5` -> `5 / 5` -> `1` (not `10 / (2/5)` which would be 25) - `a + b + c`, `a * b * c`, and chained relational/logical operators all group left first. For subtraction and division this matters because they are not associative mathematically: the grouping changes the result. ## Right-associative (the exceptions) A small set of operators group from the **right**: - **Assignment**: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `^=`, `|=`, `<<=`, `>>=`, `>>>=` - **Ternary conditional**: `? :` - **Unary** prefix operators (`!`, `~`, prefix `++`/`--`, unary `+`/`-`) are effectively right-associative too. ### The assignment example `a = b = 0` parses as `a = (b = 0)`. Here is why right-associativity is essential: in Java an assignment is itself an **expression** whose value is the value assigned. So `b = 0` first stores 0 in `b` and then *evaluates to* 0; that 0 becomes the right operand of the outer `=`, which stores it into `a`. Both `a` and `b` end up 0. If `=` were left-associative, the compiler would try to group `(a = b) = 0`, and `(a = b)` is a value, not a variable you can assign to, so chaining would be meaningless. ### The ternary example `a ? b : c ? d : e` groups as `a ? b : (c ? d : e)` because `? :` is right-associative — the second ternary nests inside the first's else-branch. ## Grouping, not timing (again) Associativity, exactly like precedence, decides **grouping** only. Operand **evaluation order** in Java is still strictly **left to right**. So in `f() - g() - h()`, the calls fire `f`, `g`, `h` in that order, even though the grouping is `(f() - g()) - h()`. ## How to derive answers - Same-precedence operators? Apply associativity: left for almost everything, right for assignment and ternary. - For subtraction/division, remember it is left: `(a - b) - c`. - For chained assignment, remember it is right: the rightmost assignment runs first and its value flows leftward.

  • Why does chained assignment require right-associativity to work at all?
    Because an assignment is an expression that evaluates to the assigned value. a = b = 0 must group as a = (b = 0) so the inner assignment runs first and yields 0 to assign to a. Left grouping would produce (a = b) = 0, and a value cannot be on the left of =.
  • Does the ternary operator group left or right?
    Right. a ? b : c ? d : e groups as a ? b : (c ? d : e), nesting the second ternary inside the first's else branch.

saying these in an interview costs you the question

  • Saying all operators are left-associative (assignment and ternary are right)
  • Claiming associativity matters for operators of different precedence (that is precedence's job)
  • Evaluating 10/2/5 as 10/(2/5)=25
  • Thinking a = b = 0 leaves b unchanged

context