i = i++; // line 3
Mike Gershman
SCJP 1.4, SCWCD in process
1. copy the value of i from memory to the top of the stack (top of stack = 11)
2. add 1 to the value of i in memory (i = 12)
3. pop the top of the stack and copy the value to i in memory (i = 11)
The key points are that post-increment adds 1 to the variable immediately after it is referenced and that the assignment to the left-hand variable occurs after the right-hand expression hads been fully processed
Given Mike's clarification, the "x++" evalutes to 3 and assigns 4 to x, and the "++x" assigns 5 to x and evaluates to 5. Thus y = 3 + 5 == 8.Originally posted by Mark Spritzler:
For instance,[ ... ]
So the pre-increment is done to make x=4, so at that point 4 + 4 = 8, and this gets assigned to y. Then the post increment on x is performed to make x=5.
I didn't try this earlier, but coming from a C / C++ background I wouldn't have expected the output to be 11, so I wrote a little test in VC++.
If similar code were written in C++ (or simply C for that matter) the result would be different (and expected), e.g.:
code:
--------------------------------------------------------------------------------
#include "stdafx.h" int main(int argc, char* argv[]){ int i = 10; i = ++i; i = i++; printf("i = %d\n", i); return 0;}
--------------------------------------------------------------------------------
Output: 12
Sun must have documented this behavior somewhere, my question is where? Is this trivial or foundational?
Maybe I am totally missing some concept here?
42
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
In C++ the outcome of i=i++; is undefined. Your version of VC++ yields i++; as the result, another might yield i;
In Java it is defined to yield i; which is preferable.
Remember Java != C++
For example, y=x++ is equivalent to y=(t=x,x+=1,t), where t is a variable of the same type as x.
Mike Gershman
SCJP 1.4, SCWCD in process
Mike Gershman
SCJP 1.4, SCWCD in process
For example, y=x++ is equivalent to y=(t=x,x+=1,t), where t is a variable of the same type as x.
JLS (�15.14.1)
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) 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.
15.7.2 Evaluate Operands before Operation
The Java programming language also guarantees that every operand of an operator (except the conditional operators &&, ||, and ? : ) appears to be fully evaluated before any part of the operation itself is performed.
Mike Gershman
SCJP 1.4, SCWCD in process
Actually, according to Stroustrup's The C++ Programming Language,
quote:
--------------------------------------------------------------------------------
For example, y=x++ is equivalent to y=(t=x,x+=1,t), where t is a variable of the same type as x.
--------------------------------------------------------------------------------
Note that z=(alpha,beta,gamma) says to evaluate expressions alpha then beta then gamma in strict sequence and assign the result of evaluating expression gamma to z.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |