File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Fendel Coulptz
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Fendel Coulptz
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks -
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My purpose in posting the example code above was to point out the mistake in that sentence.
 
Rick Portugal
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1332
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 756
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Also read this:

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

Bye,
Viki.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic