Java expressions are evaluated following the following rules:

Simple Example

In the following example:

int i = method1() + method2();

the order of evaluation is:

  1. The left operand of = operator is evaluated to the address of i.
  2. The left operand of the \\+ operator (method1()) is evaluated.
  3. The right operand of the \\+ operator (method2()) is evaluated.
  4. The \\+ operation is evaluated.
  5. The = operation is evaluated, assigning the result of the addition to i.

Note that if the effects of the calls are observable, you will be able to observe that the call to method1 occurs before the call to method2.

Example with an operator which has a side-effect

In the following example:

int i = 1;
intArray[i] = ++i + 1;

the order of evaluation is:

  1. The left operand of = operator is evaluated. This gives the address of intArray[1].