The key here is the table in "1.4 Expressions", and "7.3.1 Operator precedence and associativity". I won't duplicate the table from 1.4, but to quote 7.3.1:
- Except for the assignment operators,
all binary operators are
left-associative, meaning that
operations are performed from left to
right. For example, x + y + z is
evaluated as (x + y) + z.
- The
assignment operators and the
conditional operator (?:) are
right-associative, meaning that
operations are performed from right to
left. For example, x = y = z is
evaluated as x = (y = z).
The first is logically expanded (or: use the associativity rules) as:
i = i + ++i;
here, the order (from the table) is pre-increment, then additive, then assignment - so we should expect i to double plus one. And indeed, with i=6
, we get 13 as expected.
a[++i] = i;
again from the table, order should be array access, pre-increment, assignment - so I would expect the i+1'th value to be i+1. And indeed, checking:
int[] a = { 0, 0, 0, 0, 0 };
int i = 2;
a[++i] = i;
we do indeed get {0, 0, 0, 3, 0}
.
With the last, method invocation takes priority over subtraction, then it is left-to-right; so it should be fun()
, gun()
, -
, assignment.