• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Increment Operator pre/post fix  RSS feed

 
Kavita Gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would really appreciate getting an explanation of why the following happens..

int x=20,y=35;
x= y++ + x++;
y= ++y + ++x;
// x, y => 56, 93

As far as I know, the above statements can be explicitly written as below:

x = y + x;
y++;
x++;
y++;
x++;
y = y + x;
// x, y => 57, 94

Why does Java return different results? Or am i doing something wrong??

Thanks!

-K
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer is "Don't write code like that". However apparently there are exams which teach people about awful nonsense like that, so for that we have this FAQ entry: Post Increment Operator And Assignment. But remember, don't write code like that.
 
Kavita Gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link. It was actually an interview question that my husband was asked recently.
And I will remember not to write code like that
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, apparently there's a whole industry devoted to teaching people how to understand bad Java code. If I were to encounter that code in real life and have to work with it, my approach would be to delete the whole thing and write it over again in a more understandable way. But if you have to pass an exam or an interview, you have to deal with this sort of thing until you can get past it and into real life.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But to answer the question I believe that the 2nd line x is added to y before it is incremented and the result assigned to x so the result is 56 not 57 or to put it another way:
z = y + x
y++
x++
x = z
 
joy b chakravarty
Ranch Hand
Posts: 62
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may sound a bit nerdy here but you could see how your code runs in the background..

javac YourProgram.java
javap -c YourProgram

Try it..
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kavita Gupta wrote:Thanks for the link. It was actually an interview question that my husband was asked recently.

Asking people these sorts of questions for a job interview is crazy. As if it is important to understand this kind of code to be a good Java programmer...
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unnar is right. The x++ is negated by the assignment to x.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!