File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why??" Watch "why??" New topic
Author

why??

Howard Kung
Greenhorn

Joined: Jul 22, 2002
Posts: 24
why does
int i = 0;
i= i++;
i= i++;
i= i++;
System.out.println(i); // prints 0
how? why?


Howard Kung<br />SCJP 1.2<br />SCWCD<br />IBM Certified Specialist: WebSphere 4.0
Madhu Madala
Greenhorn

Joined: Jul 26, 2002
Posts: 9
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

Joined: Jul 22, 2002
Posts: 24
Thanks so much! It's all clear now.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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
Prashant Rane
Greenhorn

Joined: Jul 22, 2002
Posts: 2
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

Joined: Jul 02, 2002
Posts: 1865
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
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
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
The above program, class B, prints 1,1,1,0,3.
Would anyone like to post an explanation?
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Here's another one.

I'll put the result in the next post.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The above program, class C, prints 1,2,3,3,6.
Rao Rekha
Greenhorn

Joined: Jul 29, 2002
Posts: 11
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

Joined: Aug 26, 2001
Posts: 7610
Outstanding explanation about the ++ operator by Maha Anna: http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why??