Hi I think it works like this. int i=0; i=i++; i is assigned value 0 and then value of i is increamented to 1. However this value ( 1 ) is not assined back to i. It remains in some memory location which can not be accessed by i. Hence by System.out.println(i) statement 0 is printed as that is the value that has been assined to i. Hope this clarifies. Regards Sandip

nachiket deshpande

Ranch Hand

Posts: 114

posted 16 years ago

Hi Deepak! The o/p you see is because of postfix and prefix operators in assignment expressions. int i=0; i=i++; will give you the o/p 0 because i is assigned the value 0 first and then incremented. but if i do: int i=0; i++; System.out.println(i); my output is 1 and not 0.because i is incremented to 1 without any assignment. Hope this help.

Deepali Aphale

Greenhorn

Posts: 8

posted 16 years ago

I am confused regarding the answer as i=0, because is it not that, i=i++ refers to common variable i, hence will it not lead to value as 1?

Manfred Leonhardt

Ranch Hand

Posts: 1492

posted 16 years ago

Hi Deepak, Try using the java ranch search feature. This issue has been discussed numerous times in any forum here at the ranch! Both Sandip and Nachiket have the right idea but the order is reversed. The order of precedence is increment then assign. The twist here is that the increment is a post-fix notation. What does that mean? Well it means that the current value is used before the increment happens in the existing formula. In this case, the increment is nullified because we are assigning back to the original variable! The steps (in correct order!): 1. Store current value of i into memory location (store --> 0) 2. Increment current value of i (i = 1) 3. Assign stored value from step 1 to i (i becomes 0) The interesting thing is that no matter how many times you perform i = i++ the value of i will never change! Try this.

Regards, Manfred.

Li Yi

Greenhorn

Posts: 8

posted 16 years ago

according to Java Language Specification: "the value 1 is added to the value of the variable and the sum is stored back into the variable... The value of the postfix increment expression is the value of the variable before the new value is stored" Therefore after i++, even i has the value of 1, but i++ is still has value of 0, i=i++ just reassigned 0 back to i. Hope that explains.

[This message has been edited by Li Yi (edited June 22, 2001).]

Julia Costener

Greenhorn

Posts: 2

posted 16 years ago

According to THINKING IN JAVA, There are two versions of each type of operator, often called the prefix and postfix versions. For pre-increment and pre-decrement, (i.e., ++a or --a), the operation is performed and the value is produced. For post-increment and post-decrement (i.e. a++ or a--), the value is produced, then the operation is performed. As an example: public class AutoInc { public static void main(String[] args) { int i = 1; prt("i : " + i); prt("++i : " + ++i); // Pre-increment prt("i++ : " + i++); // Post-increment prt("i : " + i); prt("--i : " + --i); // Pre-decrement prt("i-- : " + i--); // Post-decrement prt("i : " + i); } static void prt(String s) { System.out.println(s); } } ///:~ The output for this program is: i : 1 ++i : 2 i++ : 2 i : 3 --i : 2 i-- : 2 i : 1 You can see that for the prefix form you get the value after the operation has been performed, but with the postfix form you get the value before the operation is performed. Hope it helps............... ------------------ Regards, Julia Costener