# what are the values in (i++ == ++i)?

Fendel Coulptz
Greenhorn
Posts: 24
hi,
say...

int i = 5;

what is being compared in (i++ == ++i) ?

is it 5 == 7 or 5 == 6 or? why?

thanks...

Nigel Browne
Ranch Hand
Posts: 703
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
Posts: 24
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.

right?
[ September 23, 2004: Message edited by: fendel coulptz ]

Nigel Browne
Ranch Hand
Posts: 703
yes if you evaluate
++i == i++ what is being evaluated is 6 == 6 the answer will be true
but the next time you use i the value will be 7

Dirk Schreckmann
Sheriff
Posts: 7023

Fendel Coulptz
Greenhorn
Posts: 24
thanks -

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by Nigel Browne:

Although it may appear that 5 == 7 is being evaluated, what is actually being evaluted is 6 == 7 the answer being false.

Huh? Why?

Dirk Schreckmann
Sheriff
Posts: 7023
My purpose in posting the example code above was to point out the mistake in that sentence.

Rick Portugal
Ranch Hand
Posts: 243
In the expression (i++ == ++i), 5 is being compared to 7. You can prove it by printing out the values:5 7

Stefan Wagner
Ranch Hand
Posts: 1923
well - I don't see any '==' in the System.out.

Is the order of evaluation for 'exp1', 'exp2' defined for
?

I wouldn't rely on experiences in this case, and avoid the whole construct as bad coding-style.

Jeroen Wenting
Ranch Hand
Posts: 5093
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.

Stefan Wagner
Ranch Hand
Posts: 1923
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
Posts: 14112
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
Posts: 14112
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

Agreed.

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.

Stefan Wagner
Ranch Hand
Posts: 1923
Thanks, Ilja.

But I'm not interested in knowing such details by heart.

Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
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
Posts: 703
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
Posts: 24
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
Posts: 14112
Originally posted by fendel coulptz:
ok, so,

int j = 10;
j++ == j++;

is "j++==j++" actually comparing 10 == 11?

Yes!

Mike Gershman
Ranch Hand
Posts: 1272
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.

Vikrama Sanjeeva
Ranch Hand
Posts: 760
Hi,

http://www.coderanch.com/t/204315/java-programmer-SCJP/certification

Bye,
Viki.

Ilja Preuss
author
Sheriff
Posts: 14112
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
Posts: 54
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.

DD