This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes x++ , ++x Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "x++ , ++x" Watch "x++ , ++x" New topic
Author

x++ , ++x

Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
I am not sure why, what is going on here is not getting through to me.


Produces this output.

When ++y x = 2 and y = 2
When z++ a = 1 and z = 2

why?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

++y is the pre-increment. x = ++y essentially says 'increment y, then use the new value of y to assign to x'

z++ is the post-increment. a = z++ essentially says 'use the current value of z to assign to a, then increment z'


Steve
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Thanks, I find it very helpful to see it in regular ol English.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43905
    
  33
Please do a search; that sort of problem causes no end of confusion and there are lots of similar questions here on beginners'.
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Campbell Ritchie wrote:Please do a search; that sort of problem causes no end of confusion and there are lots of similar questions here on beginners'.


I tried, but the search wont take ++ as a valid entry, and I didn't know exactly what else to look for.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43905
    
  33
Try searching for "preincrement postincrement" and you get a few hits: 1, 2 (end of discussion), 3, and there are others.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: x++ , ++x
 
jQuery in Action, 3rd edition