• 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

post increment operator

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if this question is so easy it is stupid, but Im new to Java and cannot wrap my mind aroud why:
int x = 5;
x = x++;
System. out.print (x);
produces 5. What happens with post increment?
Thanks
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alexander:

Welcome to JavaRanch!

No, your question is not dumb . The reason why you're getting 5, is that the postfix increment happens after the assignment. So, in your example what happens is:

You create x and assign it a value of 5:

You then assign x the value of a temporary variable equal to x++. In detail, you are assigning x the value of the temporary variable, then incrementing the temporary variable (which gets dropped):

Naturally, when you print it out, you get 5


If you want to get six, you probably should just do this:



John.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alexander,

welcome to the JavaRanch ;-)

For your question: Just have a look at the JavaRanch FAQ. It explains exactly your problem!

Marco
 
Alexander Danilou
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, looks like I need to start by learning how to use serch in FAQ..
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Searching for existing knowledge on the web is always a good idea

But as a general advice: If you want to learn Java you don't have to focus on such subtle details of the language syntax. If you take an exam or something like this then you probably will have to learn these things but for practical usage of Java there are other much more important things besides difficult to understand code fragments like this. Of course such an language constructs may be useful in some situations as you can see they often lead to confusion. And believe me, this is not only true because you are new to Java If you use a modern object oriented language like Java wisely you hopefully won't see such things very often! Today higher level concepts and clean design and architecture of a software are much more important than using tricky operators.

Marco
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch. It is worthwhile searching for "postincrement" in this forum; similar questions come up frequently, probably on average one a month at least.

And it is very easy to get confused about the postincrement operator.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic