File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected behaviour of post-increment : what is happening?

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic