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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "any precise answer ?" Watch "any precise answer ?" New topic

any precise answer ?

Deepak Shah
Ranch Hand

Joined: Nov 29, 2000
Posts: 97
The code ,
int i=0;
i = i++;

prints 0
why not 1.
expecting precise answer !!
Thanks in advance !!
Deepak Shah
sandip karmarkar

Joined: Jan 08, 2001
Posts: 26
I think it works like this.
int i=0;
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.
nachiket deshpande
Ranch Hand

Joined: Oct 06, 2000
Posts: 114
Hi Deepak!
The o/p you see is because of postfix and prefix operators in assignment expressions.
int i=0;
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;
my output is 1 and not 0.because i is incremented to 1 without any assignment.
Hope this help.
Deepali Aphale

Joined: May 28, 2001
Posts: 8
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

Joined: Jan 09, 2001
Posts: 1492
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.

Li Yi

Joined: Jun 21, 2001
Posts: 8
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

Joined: Jun 23, 2001
Posts: 2
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) {
} ///:~
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...............
Julia Costener

Regards,<BR>Julia Costener
I agree. Here's the link:
subject: any precise answer ?
jQuery in Action, 3rd edition