When using post-incremation as is the case with i++ the value is produced before the operation takes place. So printing out the value of i on the line of i++ will print in your case the value 5. However the value is incremented and when you arrive at the pre-increment ++i, i is increased from its current value which is 6 before the value is produced. This results in the value for i being 7. Although it may appear that 5 == 7 is being evaluated, what is actually being evaluted is 6 == 7 the answer being false.
Hope this clears things up
Joined: Sep 23, 2004
righto, thank you!
it means for i++, i is incremented only AFTER the CURRENT immediate operation, right...
for i++ and ++i if it's
both basically results in incrementing i, and the pre- and post- fix does not apply cos its fully evaluated.
Although it may appear that 5 == 7 is being evaluated, what is actually being evaluted is 6 == 7 the answer being false.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Dec 10, 2001
My purpose in posting the example code above was to point out the mistake in that sentence.
I'm quite sure that the behaviour of Java is well specified in this case (in contrast to C++, as far as I know).
Actually in this case C++ is well specified ... thus the old joke about C++ being "an incremental addition to C that evaluates to the same thing".
Joined: May 15, 2001
I stand corrected, 5 == 7 is evaluated, although the value of i is 6 before it reaches the prefix increment operation. I retract my sentence that 6==7 was being evaluated, and would like to thank those who took the time to show me the error of my ways
Joined: Sep 23, 2004
int j = 10; j++ == j++;
is "j++==j++" actually comparing 10 == 11?
left side: j++ is 10, just after "==" it is increased to 11 right: so "j++" uses 11 after ";" : j increments, 11 becomes 12
It might be easier to understand these expressions if we looked at them in reverse Polish notation like a compiler:
i++ == ++i <==> i++ ++i ==
j++ == j++ <==> j++ j++ ==
The left operand is retrieved and its value saved for the == operation, then the post-increment operation is performed on the left operand, then the right operand is processed, then the saved values are compared. This is a deterministic consequence of == being left-to-right associative, not a function of your choice of Java compiler.