File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes what are the values in (i++ == ++i)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what are the values in (i++ == ++i)?" Watch "what are the values in (i++ == ++i)?" New topic
Author

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

Fendel Coulptz
Greenhorn

Joined: Sep 23, 2004
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

Joined: May 15, 2001
Posts: 673
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
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

Joined: May 15, 2001
Posts: 673
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

Joined: Dec 10, 2001
Posts: 7023


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Fendel Coulptz
Greenhorn

Joined: Sep 23, 2004
Posts: 24
thanks -
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
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?


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
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
In the expression (i++ == ++i), 5 is being compared to 7. You can prove it by printing out the values:5 7


IBM 286, SCJP, SCWCD, EIEIO
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
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.


http://home.arcor.de/hirnstrom/bewerbung
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
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.


42
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
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

Joined: Jul 11, 2001
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

Joined: Jul 11, 2001
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

Indeed very bad coding style


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

Joined: Jun 02, 2003
Posts: 1923

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: 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

Joined: May 15, 2001
Posts: 673
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
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
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
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.


Mike Gershman
SCJP 1.4, SCWCD in process
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
Hi,

Also read this:

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

Bye,
Viki.


Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
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

Joined: Oct 12, 2004
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


SCJP 1.4<br />SCWCD (in progress)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what are the values in (i++ == ++i)?