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...
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.