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
Fendel Coulptz
Greenhorn
Joined: Sep 23, 2004
Posts: 24
posted
0
righto, thank you!
it means for i++, i is incremented only AFTER the CURRENT immediate operation, right...
for i++ and ++i if it's
++i;
and
i++;
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.
Huh? Why?
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
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
0
My purpose in posting the example code above was to point out the mistake in that sentence.
Rick Portugal
Ranch Hand
Joined: Dec 17, 2002
Posts: 243
posted
0
In the expression (i++ == ++i), 5 is being compared to 7. You can prove it by printing out the values:5 7
Not quite sure, but I THINK the i++ in this case is executed AFTER both the == and ++i yielding a comparison if (5==6) after which i is equal to 7.
I doubt there's any way to find out for certain though (maybe the bytecode will help here, stepping through it in a debugger certainly won't as the line is an atomic operation).
Indeed very bad coding style, and the printout example does not yield this result as it consistst of 2 expressions containing i instead of 1.
The bytecode will not help, when the specification isn't clear, because it may change from implementation to implementation.
But I don't like to search for the specification.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
I'm quite sure that the behaviour of Java is well specified in this case (in contrast to C++, as far as I know).
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Jeroen Wenting: Not quite sure, but I THINK the i++ in this case is executed AFTER both the == and ++i yielding a comparison if (5==6) after which i is equal to 7.
Nope, i++ is executed first, but it's value is that of i before the increment.
I doubt there's any way to find out for certain though.
Thanks, Ilja. I had a look at the links, and well read for half an hour interesting things.
But I'm not interested in knowing such details by heart.
Warren Dew
blacksmith
Ranch Hand
Joined: Mar 04, 2004
Posts: 1328
posted
0
Ilja Preuss:
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".
Nigel Browne
Ranch Hand
Joined: May 15, 2001
Posts: 673
posted
0
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
Fendel Coulptz
Greenhorn
Joined: Sep 23, 2004
Posts: 24
posted
0
ok, so,
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
is this correct.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by fendel coulptz: ok, so,
int j = 10; j++ == j++;
is "j++==j++" actually comparing 10 == 11?
Yes!
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
posted
0
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.
Originally posted by Mike Gershman: It might be easier to understand these expressions if we looked at them in reverse Polish notation like a compiler:
Nice explanation, Mike!
Dun Dagda
Ranch Hand
Joined: Oct 12, 2004
Posts: 54
posted
0
Sounds like the kind of thing you see all the time on the SCJP exam, so if you are taking the exam, it would be good to have a thorough understanding of the topic.