This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hello All! I am going to try my best to dissect the post-fix operator. First lets see what some people in the past have to say about this operator: --------------------------------- Yuhri Hirata (June 15, 2000): i = (i++); The reason that the i++ doesn't increment despite the parentheses is that the purpose of the operator itself, ++, is to increment /after/ its use in the expression. Use of the parentheses won't negate that purpose. Deepak M (July 25, 200): Three steps in evaluting a = a++; 1. return value of a 2. increment a 3. assign the returned value of a in step 1 to LHS of = operator. groggsy (August 30, 2000): int x=0; x=x++; x Starts with 0 The right hand side of the = is evaluated (++ is post increment, so x is not incremented yet) 0 is put into a storage area which I will call - Store. x is then incremented to 1 (because of ++). Jane (August 31, 2000): i = i++; The assignment operator in the original example must somehow cause the postfix operation to complete abruptly; causing it to fail. robl (September 11, 2000): I would cahracterize this one as a bug in the JVM. -------------------------------- These are only couple of things I could collect after doing a search on "Postfix". There were several more posts that I did not go through and hence the myth that I am about to dispel now may have already been dispelled. Let me start this thread with the following code:
The output of the above code is:- ----- After first increment a = 6 After second increment and assignment a = 6 a = a++ + a results in a being: 13 a + a++ = 26 In the end a = 14 ---- Lets take a look at couple of things from the JLS Section 15.14.1 before I start rambling: A. Also, as discussed in �15.8, names are not considered to be primary expressions, but are handled separately in the grammar to avoid certain ambiguities. B. The value of the postfix increment expression is the value of the variable before the new value is stored. Statement/Code Analysis: 1. The value of this postfix increment operation is 5. Please note that this increment expression is not assigned to any variable. In this statement the post fix increment expression is evaluated. The result of this evaluation i.e. "a = a + 1" is the value of "a"(variable a) is incremented and assigned to "a" i.e. a = 6 now. 2. This is the most notorious statement which took my afternoon!! Several things are happening here. Please keep in mind the statement B above and lets analyze: a = a++; This is an explicit assignement statement with Right hand side being a expression. Since the associativity of "=" is from right to left the right hand side expression must be evaluated first. Now, one might think that all right lets increment the variable a on the right hand side and assign it back to a. Well why not, because the post fix operator definitely has the higher precedence. Well that's not exactly what happens even though the precedence of "++" is higher than "=". Here is what I believe happens: a++ is evaluted to the value stored in the variable "a" before incrementing. So, now we have two expressions: a = 6 and a = a+1 (i.e. the execution of post-fix operation) Since the post-fix operation has the higher precedence it is executed first, i.e. a = a + 1 i.e. 7 is stored in a. After that a = 6 is executed. Let me try to explain this wierd behavior in a better way: For a moment assume that a++ is a function f() that always returns the value in "a" before incrementing. So, a = f(); What happens in the function, in this assignment case, is irrelevant. What is important is what value is returned from the function. As seen from statement B above the value of the post-fix operator is the value of the variable before the increment occurs. Hence, I strongly believe that the value of a is updated twice in the statement 2 of the code. First a = a+1 i.e. a = 6 + 1. and then final value is "a = 6" however. You can call this a side-effect. I believe it will help you if you see a++ as a function f() which works on a and returns a before it increments it. 3. The dreadly statement: a = a++ + a; Remember the post-fix operator has a higher precedence . So a++ is evaluated first, and it returns the original value of a i.e. 6. Or rather I should say, a++ returns 6 first and then a++ is executed. When a++ is executed it results in "a" being 7 but don't forget that the operation had returned the value of 6 and that's what will be used for a++. You can call this a side-effect. Now wait, don't go ahead and assign it to the variable "a" yet. Remember the addition operation is left and it has higher priority than the assignment operation. So, the value of a after a++ is executed is added to "a". i.e. 6 + 7 Hence you get 13. 4. In statement no. 4 of the code there is no explicit assignment statement. The evaluation occurs in the following manner: 13 + 13(14) i.e. 13 is returned by a++ and after that a's value is incremented to 14. 5. In the end it does print a's value to be 14 even though there was no explicit assignemnt on statement 4. I hope this helps, Constructive criticism is welcome, Brian PS - Don't forget Maha Anna's wonderful discussion on the post-fix operators too: http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array [Edited by Val to correct the link to Maha's topic] [ October 09, 2002: Message edited by: Valentin Crettaz ]