Howard, I saw answer some where in discussion forums It is connection with assigning a value. 1.int i=0; 2.i=i++; 3.i=i++; 4.i=i++; System.out.println(" int i = "+ i); //prints out int i = 0 Because 0 will be assigned first to i now in 2.again 0 is assigned to i and i is incremented.
Now you run this code int i=0; i++; i++; i++; System.out.println("int I = " + i); //The out put will be int I = 3
1 int i=0; 2 i=i++; 3 i=i++; 4 i=i++; It is not clear to me how i is 0 .In line 2 first i is assigned 0 then it is incremented.In line 3 incremented value of i should be assigned ,right? That is in line 3 1 is assigned to i & then again it gets incremented to 2. Veena
SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
I agree with Veena. Here are my two sample test scenarios: ############################################ public class testIncr { public static void main (String[] args) { int a = 0; int b = 0; b=a++; System.out.println (a); System.out.println (b); } } When executed prints: 1 0
########################################### public class testIncr { public static void main (String[] args) { int a = 0; int b = 0; a=a++; // note the change System.out.println (a); System.out.println (b); } } When executed prints: 0 0 #############################################
Why? Why? Why? I even looked at JLS, but no clue about this behavior. Please explain. Thanks, Prashant Rane
Rather than look for the answer in the Java Language Specification I believe the answer is more likely to be found in the Java Virtual Machine specification. I've only looked at it briefly, so I'm certainly no expert on the JVM. However, I do know that the JVM does arithmetic on the stack like a Hewlett Packard calculator. For example, to add two numbers it will push the two values on the stack and then do the add operation. If you apply similar logic to the use of the postfix operators we can speculate on the evaluation of the following statement. i = i++; The postfix operator is evaluated by first preserving the value of i by pushing it onto the stack. Evaluation of the postfix operator is completed by incrementing the value of the local variable i. The next operator to be evaluated is the simple assignment operator. The evaluation is accomplished by popping the original value of i off of the stack and by assigning the original value to local variable i. If the above is true, the value of i changes twice. The first time is due to the increment operation that is the side-effect of the postfix operation. The second modification of i is due to popping the original value of i off of the stack to complete the evaluation of the simple assignment operator. The unexpected result is due to the fact that the postfix operation is completed before the simple assignment operation. Therefore, the simple assignment operation wins.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
posted
0
The following program illustrates the operation of the postfix operator.
The JVM first evaluates the postfix expression. The result is the original value of i which is zero. Next, the value of i is incremented. The right hand operand of the addition operator is the return value of the method, m. Method m prints the current value of i which is one. Then m returns zero. The result of the addition is also zero and that value is assigned to local variable i. In summary, the value of i changed twice. First, it was incremented as a result of the postfix operation. Second, i was assigned the value resulting from the addition operation. I think I'll add this one to my mock exam.
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
posted
0
Here's another fun one that I'll add to my exam. What does the following program print?
I'll post the result in the next post so that you won't have to see it before you are ready.
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
posted
0
The above program, class B, prints 1,1,1,0,3. Would anyone like to post an explanation?
Originally posted by Veena Point: 1 int i=0; 2 i=i++; 3 i=i++; 4 i=i++; It is not clear to me how i is 0 .In line 2 first i is assigned 0 then it is incremented.In line 3 incremented value of i should be assigned ,right? That is in line 3 1 is assigned to i & then again it gets incremented to 2. Veena
Because, the first time it assigns zero to i and then increments the value of i on the right, which is immaterial, because, it has already been assigned.
So, in the second step, i retains the value that was initially assigned, which is 0.