April 23, 2011, 5:06 a.m.
posted by keepapple
Evaluation Order of Operands
In order to understand the result returned by an operator, it is important to understand the evaluation order of its operands. Java states that the operands of operators are evaluated from left to right.
Java guarantees that all operands of an operator are fully evaluated before the operator is applied. The only exceptions are the short-circuit conditional operators &&, ||, and ?:.
In the case of a binary operator, if the left-hand operand causes an exception (see Section 5.5, p. 181), the right-hand operand is not evaluated. The evaluation of the left-hand operand can have side effects that can influence the value of the right-hand operand. For example, in the following code:
int b = 10; System.out.println((b=3) + b);
the value printed will be 6 and not 13. The evaluation proceeds as follows:
The evaluation order also respects any parentheses, and the precedence and associativity rules of operators.