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
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.
[ September 23, 2004: Message edited by: fendel coulptz ]
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.
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.
Well, you could read http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4779 and http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#36254
Indeed very bad coding style
the printout example does not yield this result as it consistst of 2 expressions containing i instead of 1.
Disagreed - a String concatenation is an expression, too, and the same rules apply.
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".
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.
Also read this: