• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Increment Operator pre/post fix

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28257
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28257
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 164
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 62
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unnar is right. The x++ is negated by the assignment to x.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic