Reading the JLS a bit more, I found this:
<
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26.1>
"Otherwise, three steps are required:
* First, the left-hand operand is evaluated to produce a variable.
If this evaluation completes abruptly, then the assignment expression
completes abruptly for the same reason; the right-hand operand is not
evaluated and no assignment occurs.
* Otherwise, the right-hand operand is evaluated. If this
evaluation completes abruptly, then the assignment expression completes
abruptly for the same reason and no assignment occurs. "
"Otherwise" means if the left hand operand of an assignment is neither a
field or an array.
But the point is the operand is evaluated "first to produce a variable."
This is implies to me that there are two passes over a Java
expression. First, to produce variables. This pass seems to go left to
right. (I can't think of a case where grouping or precedence would make
it go differently.)
Then in the second pass the variables are evaluated with the specified
order of operations. All expressions are evaluated here.
The exception in both cases seems to be the short circuit operators,
which may not evaluate their right hand variables or operations at all,
depending on the value of the left hand expression.
To me, that latter bit implies that expressions involving || and &&
might evaluate differently than the OP's example.
Getting back on track a bit:
Joshua said:
> Generated bytecode (roughly):
>
> iconst_4 push 4 on to the stack
> istore <index> store the top of the stack (4) into index
> aload <z> load z to the stack
> iload <index> load index (4) on the stack
> iconst_2 push 2 on to the stack
> dup duplicate the top element (2) on the stack
> istore <index> store the top of the stack (2) into index
> iastore store the top of the stack into the index denoted by
I think the compiler could choose to evaluate "aload <z>" in a different
order of the JLS were different. Get rid of the iconst_4 and move the
evaluation of aload <z> down a bit and you get the effect the OP seems
to be expecting. It's even one less byte code, I think. The compiler
is doing extra work to conform to the JLS.