It's not a secret anymore!
The moose likes Beginning Java and the fly likes order of evaluation/execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "order of evaluation/execution" Watch "order of evaluation/execution" New topic

order of evaluation/execution

Tony Jackson
Ranch Hand

Joined: Feb 23, 2001
Posts: 45
Someone recently pointed out that the following prints out the number 10, not 11 as expected:
int i = 10;
i = i++;
System.out.println("i: " + i);
my "guess" as to what is happening:
1. i (on the RHS) is evaluated (it is 10).
2. i (the variable) is incremented (becomes 11).
3. The assignment (due to the equals sign) occurs, using the evaluated value from step 1, not the variable.
But if that is the case, then why does the post increment occur before the assignment?
Thanks for any insight!
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
i++ is occuring after the variable has been assigned to i. It's just the way java handles pre/post increment operations in an assignment statement. You are correct, to get the desired behavior, use the pre-increment.

So much trouble in the world -- Bob Marley
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Tony,
You are absolutely correct in the three steps you have written out! The post increment happens before the assignment because it has a higher priority then the assignment.
The compiler can only follow the JVM specs. It can't try and figure out what the programmer actually wanted.
Just to prove to yourself that the increment happens before the assignment try the following code:

The result will be 21 because the increment has the highest priority on the righthand side. The order will be
1. Save 10 into memory
2. Increment i to 11
3. Add saved value (10) to current value or i (11)
4. Print out result --> 10 + 11 = 21
Remember, the JVM compiler will always use the priority table and the assignment will (almost always) be performed last!
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

First, I admit to posting a completely different message about half an hour ago, and I've edited this one.. so apologies to anyone that read that old one and wanted to refer to it. But really, it was kinda muddled anyways.

Here is the only part of the old post that survived:
I think this might be the answer (from RHE):
In Java, unlike many other languages, the apparent order of evaluation of operands in an expression is fixed. Specifically, the result of any statement will be as if all operands are evaluated left to right, even if the order of execution of the operations is something different, or if some optimization makes other changes to the reality.
So what this mostly says to me is... now matter what the precedence is, always perform atomic units of 'evaluation' from left to right. Also note, that in the first example given, there is only one 'operand', and I think that things are made more clear when there are two evaluations, and also when you compare the results of assigning to 'self' and assigning to 'not self'...
It is helpful to also write down my thoughts as I go through one example. I've made it a code block so it keeps indenting.

[This message has been edited by Mike Curwen (edited April 17, 2001).]
Fred Hosch

Joined: Apr 16, 2001
Posts: 23
Just a matter of precedence. ++ has higher precedence than =.
java guarantees that every operand "appears to be fully
evaluated " before any part of the operation is performed.
the postfix increment assigns 11 to i, but delivers 10 as
its value. this is tehn assigned to i.
interestingly, this statemetn is not permitted in ansi C.
Fred Hosch
Author of:
An Introduction to Software Construction with Java

Fred Hosch<br />Author of:<br /><a href="" target="_blank" rel="nofollow">An Introduction to Software Construction with Java</a>
I agree. Here's the link:
subject: order of evaluation/execution
jQuery in Action, 3rd edition