int i = 10, j = 5, k;
k = f(++i) + g(++i) + j+ 25;
Is f(++i)
or g(++i)
is computed first?
and how can you know without running the program?
Assuming f
and g
are similar to
int f(int a)
{ return a; }
How does the order change depending on the compiler?
Which method compiler executes fist?
It is not the compiler that executes instructions but the JVM.
And the Java programming language guarantees that the operands of operators are evaluated from left to right by the JVM. From JLS Sec 15.7:
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
About whether the compiler may reorganize the order of equivalent expressions in the compiled class, it may be the case for "optimization" reasons. These may change from one Java version to another one, so generally you should even not consider it.