I'm having trouble understanding why the output of the following is 5 instead of 4:
I know that the postfix operators affect the value AFTER the assignment. But since the variable, a, IS the variable being decremented, shouldn't it be affected by the -- after the assignment of the value of 5? This is clearly not the case. Why not? What is going on here?
Trey Carroll<br />======================<br />Studying for the SCJP
Hi ,Go through coreys scjp tipline where there are excellent articles about various topics including pre and postfix operators to get a clear understanding.It helped me! [ October 02, 2008: Message edited by: sumi rankan ]
Joined: May 06, 2008
Thanks for the feedback. I'm afraid that I haven't made my question clear or that the subtlety of this question has been overlooked.
I understand the difference between prefix and postfix increment/decrement operators. If the original code were replaced with this:
I would have no problems at all. However, my question is this:
If we think of a as a labeled 32 bit bucket, then the code:
brings up a strange situation.
So far, so good, but then, after the assignment is complete, the -- is applied to the value stored in a!
Am I missing something here? After the assignment the -- is supposed to decrement the value stored in a.
The only scenario I can come up with to understand this output is to hypothesize that the actual process here runs like this:
1) evaluate the current value of in a and store this for later use in the assignment. 2) allow the decrement operation to run 3) assign the previously stored value to variable.
Thus, the diagram for my conjecture would be:
Can anyone confirm this or offer an alternative explanation? [ October 02, 2008: Message edited by: Trey Carroll ]
Joined: May 06, 2008
Corey's Tipline was exactly what I needed. (He confirms my hypothesis.)
Also, Thanks to you, Lakuma. Your example:
a = a++ + a;//a = 11
Shed light on the process too. It taught me that the last step is always the assignment so increments and decrements will never "write through" to the LHS.
I got the impression from atleast one post in here that the increment from a++ never gets writtent back to a, so I'm adding my comments.
Take note of the following two points from the JLS & Java's operator precedence * 15.7.1 Evaluate Left-Hand Operand First - The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated. * 15.14.2 Postfix Increment Operator ++ - The value of the postfix increment expression is the value of the variable before the new value is stored * Operator precedence - http://www.irixtech.com/java/tutorials/java-operator-precedence
So, for the example, from the original post "a = a--", a-- puts 5 as the value in the expression & makes the final value of a as 4 per JLS 15.14.2. And now, since assignment will always happen in the end the final value of a ends up being 5 although a was 4 after a--.
Another example to consider is "a = a++ + a". In this case, per JLS 15.7.1 left-hand operands need to be fully evaluated before any part of the right-hand can be evaluated. So we evaluate a++, we get 5 in the expression & a has the value 6. Now we evaluate the right-hand operand which is simply substituting the value of a i.e. 6. Finally, the assignment is performed & a ends up being 11.
Take a minute to have a look at the said JLS articles, the JLS may seem geeky at times but all the answers are there. JLS 15.7.1 JLS 15.14.2
Originally posted by Ashish Hareet: Guys, So, for the example, from the original post "a = a--", a-- puts 5 as the value in the expression & makes the final value of a as 6 per JLS 15.14.2. And now, since assignment will always happen in the end the final value of a ends up being 5 although a was 6 after a--.
I think you wrote 6 instead of 4. if a is 5 and you do a--, it will not result in 6, it will result in 4. But your explanation was good....