ipkiss - 10 months ago 54

Java Question

I am reading some Java text and got the following code:

`int[] a = {4,4};`

int b = 1;

a[b] = b = 0;

In the text, the author did not give a clear explanation and the effect of the last line is:

`a[1] = 0;`

I am not so sure that I understand: how did the evaluation happen?

Answer Source

Let me say this very clearly, because people misunderstand this all the time:

**Order of evaluation of subexpressions is independent of both associativity and precedence**. Associativity and precedence determine in what order the

Consider `A() + B() + C() * D()`

. Multiplication is higher precedence than addition, and addition is left-associative, so this is equivalent to `(A() + B()) + (C() * D())`

But knowing that only tells you that the first addition will happen before the second addition, and that the multiplication will happen before the second addition. **It does not tell you in what order A(), B(), C() and D() will be called!** (It also does not tell you whether the multiplication happens before or after the first addition.) It would be perfectly possible to obey the rules of *precedence and associativity* by compiling this as:

```
d = D() // these four computations can happen in any order
b = B()
c = C()
a = A()
sum = a + b // these two computations can happen in any order
product = c * d
result = sum + product // this has to happen last
```

All the rules of precedence and associativity are followed there -- the first addition happens before the second addition, and the multiplication happens before the second addition. Clearly we can do the calls to A(), B(), C() and D() in *any* order and still obey the rules of precedence and associativity!

We need a rule *unrelated* to the rules of precedence and associativity to explain the order in which the subexpressions are evaluated. **The relevant rule in Java (and C#) is "subexpressions are evaluated left to right".** Since A() appears to the left of C(), A() is evaluated first, *regardless of the fact that C() is involved in a multiplication and A() is involved only in an addition.*

So now you have enough information to answer your question. In `a[b] = b = 0`

the rules of associativity say that this is `a[b] = (b = 0);`

but that does not mean that the `b=0`

runs first! The rules of precedence say that indexing is higher precedence than assignment, but **that does not mean that the indexer runs before the rightmost assignment**.

The rules of precedence and associativity impose the restrictions that:

- The indexer operation must run before the operation associated with the left-hand assignment operation
- The operation associated with the right-hand assignment operation must run before that associated with the left-hand assignment operation.

Precedence and associativity only tell us that *the assignment of zero* to `b`

must happen *before* the assignment to `a[b]`

. Precedence and associativity says nothing about whether the `a[b]`

is evaluated *before* or *after* the `b=0`

.

Again, this is just the same as: `A()[B()] = C()`

-- All we know is that the indexing has to happen before the assignment. We don't know whether A(), B(), or C() runs first *based on precedence and associativity*. We need another rule to tell us that.

The rule is, again, "when you have a choice about what to do first, always go left to right": the `a[b]`

is to the left of the `b=0`

, so the `a[b]`

runs *first*, resulting in `a[1]`

. Then the `b=0`

happens, and then the assignment of the value to `a[1]`

happens last.

Things to the left happen before things to the right. That's the rule you're looking for. Talk of precedence and associativity is both confusing and irrelevant.

People get this stuff wrong *all the time*, even people who should know better. I have edited *far too many* programming books that stated the rules incorrectly, so it is no surprise that lots of people have completely incorrect beliefs about the relationship between precedence/associativity, and evaluation order -- namely, that in reality there is no such relationship; they are independent.

If this topic interests you, see my articles on the subject for further reading:

http://blogs.msdn.com/b/ericlippert/archive/tags/precedence/

They are about C#, but most of this stuff applies equally well to Java.