This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes post increment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "post increment operator" Watch "post increment operator" New topic
Author

post increment operator

Alexander Danilou
Greenhorn

Joined: May 08, 2009
Posts: 28
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
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

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

Joined: May 08, 2009
Posts: 28
Thanks, looks like I need to start by learning how to use serch in FAQ..
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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.
 
 
subject: post increment operator