• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why??

 
Howard Kung
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does
int i = 0;
i= i++;
i= i++;
i= i++;
System.out.println(i); // prints 0
how? why?
 
Madhu Madala
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Howard Kung
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much! It's all clear now.
 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prashant Rane
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above program, class B, prints 1,1,1,0,3.
Would anyone like to post an explanation?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another one.

I'll put the result in the next post.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above program, class C, prints 1,2,3,3,6.
 
Rao Rekha
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Outstanding explanation about the ++ operator by Maha Anna: http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic