Reputation: 25
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?
Upvotes: 0
Views: 131
Reputation: 131346
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.
Upvotes: 1
Reputation: 2453
Unless otherwise modified, evaluation is left-to-right. So in this case, f(++i) goes first.
EDIT: As @sanA points out, there could be compiler optimizations. In your case it's unlikely (but not impossible) since your functions probably qualify as side effects.
Upvotes: 0