• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

any precise answer ?

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code ,
int i=0;
i = i++;
System.out.println(i);

prints 0
why not 1.
expecting precise answer !!
Thanks in advance !!
Deepak Shah
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic