How should postincrement work?
Let me acknowledge that thhis is more or less a cross post from another thread.
The original poster asked why the expression
i=i++; evaluates to 10 rather than 11.
In the discussion, the answer is that the assignment is made before i is incremented. I to would expect the expression to evaluate as 11.
referring to http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#4987
15.14.2 Postfix Increment Operator ++
A postfix expression followed by a ++ operator is a postfix increment expression. The result of the postfix expression must be a variable of a type that is convertible (Â§5.1.8) to a numeric type, or a compile-time error occurs. The type of the postfix increment expression is the type of the variable. The result of the postfix increment expression is not a variable, but a value.
At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (Â§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (Â§5.1.3) and/or subjected to boxing conversion (Â§5.1.7) to the type of the variable before it is stored. The value of the postfix increment expression is the value of the variable before the new value is stored.
Note that the binary numeric promotion mentioned above may include unboxing conversion (Â§5.1.8) and value set conversion (Â§5.1.13). If necessary, value set conversion is applied to the sum prior to its being stored in the variable.
A variable that is declared final cannot be incremented (unless it is a definitely unassigned (Â§16) blank final variable (Â§4.12.4)), because when an access of such a final variable is used as an expression, the result is a value, not a variable. Thus, it cannot be used as the operand of a postfix increment operator.
My feeling is that the expression
should assign a value of 10 to i. The reference is clear that a value for i is created and incremented. It also says that that this value is subsequently assigned to i unless "the operand expression completes abruptly". Perhaps this is the key to the quandry, but I have now idea what "completes abruptly" means, or how it affects the evaluation.
Adding to the problem is that
i=i++; evaluates to 10 in Java and 11 in C/C++;
Likewise, i=i + i++ evaluates to 20 in Java and 21 in C/++.
This bothers me immensly. I should think the evaluation would be the same, given the similarity between the two languages. It certainly makes porting applications more difficult.
It would seem to me that the expression i++ would increment i at some point. There is of course some ambiguity as to what happens in the expression i + i++. You can't assign the incremented value of i to i after evaluating and assigning i. That would clearly leave i in an incorrect state.
I can see that the expression i = i + i++ would initialy assign the value of 20 to i. The problem comes as to whether i should subsequntly be incremented to 21, as C evidently does.
Am looking for a clearer explanation of what is going on.