This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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! Tony (email@example.com)
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. Bosun
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
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! Regards, Manfred.
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).]
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="http://www.amazon.com/exec/obidos/ASIN/0471354899/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">An Introduction to Software Construction with Java</a>