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 Unexpected behaviour of post-increment : what is happening? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unexpected behaviour of post-increment : what is happening?" Watch "Unexpected behaviour of post-increment : what is happening?" New topic
Author

Unexpected behaviour of post-increment : what is happening?

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Please see the code and output below :



Output :



What is the logic behind making the post-increment operator work this way ? Why would someone want to use a statement like this in their code ?

PS : This one was a big shocker !!!

SCJP 6. Learning more now.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Nobody would use a statement like q = q++ in a real application. It's pointless. But for the sake of understanding...you understand the different between post-increment and pre-increment operators, right? Because that statement takes the 'value' of q++...and assigns it to q. So what would you expect the value of q to be afterwards?
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Matthew Brown wrote:Nobody would use a statement like q = q++ in a real application. It's pointless. But for the sake of understanding...you understand the different between post-increment and pre-increment operators, right? Because that statement takes the 'value' of q++...and assigns it to q. So what would you expect the value of q to be afterwards?


Ok, so it simply does this : q=q and the ++ is for show !!! I checked r = ++r , which works as expected.
I wonder how many traps of this kind are there in java...any compilation of such traps ?


PS : I just want to memorize such silly traps , get the scjp and do some useful coding instead of such silly magic tricks...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14157
    
  19

If you'd like to learn more of these traps, I highly recommend the book Java Puzzlers. It contains a lot of examples, most of which are even more confusing that this one. And you learn a lot about some tricky cases in Java while studying this book.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Rahul Sudip Bose wrote:... Ok, so it simply does this : q=q and the ++ is for show !!! ...

It's not entirely "for show." The increment happens, but is immediately undone. Here's how I understand it:

First, the right side of the assignment is evaluated. Because this is a post-increment, q++ returns its value prior to incrementing, which is 10. But this value isn't assigned to q yet, because the right side is not finished evaluating, so put that '10' aside for the moment. Next, the post-incrementing takes place, giving q a value of 11. Then, with the right side finished evaluating, that value of 10 gets assigned back to q.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
Consider Paul's rocket mass heater.
 
subject: Unexpected behaviour of post-increment : what is happening?