File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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: 39380
    
  28
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: 39380
    
  28
Try searching for "preincrement postincrement" and you get a few hits: 1, 2 (end of discussion), 3, and there are others.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: x++ , ++x