• 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

post increment operator

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all.......
int i = 0;
i = i++;
i = i++;
System.out.println( i );
why the output of above code is zero?
i guess it has something to do with internal implementation of post increment operator.
can ne1 explain?
thx in advance..........Ajit
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!!
Not so long ago we had a vey nice conversation about this.
Here you are: http://www.javaranch.com/ubb/Forum24/HTML/012305.html
Have a nice day,
Mariusz
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this
http://www.javaranch.com/ubb/Forum24/HTML/008856.html
and
http://www.javaranch.com/ubb/Forum24/HTML/008873.html
HTH
tvs sundaram
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can solve the below example as :
int i = 0;
i = i++; // i = 0(1) i.e value of i = 0
i = i++; // i = 0(1) i.e value of i still remains 0
System.out.println( i ); // prints 0
So the output is 0.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even more confusingly
int i=0;
i = (i++)+(i++);
System.out.println(i);// returns 1
How come?
Tom
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i's value is 0 at the beginning, then
i = (i++) + (i++) will be evalutated left to right.
the first i++ (on the left of the RHS) stores the value of i (i.e. 0) and that value will be used in the addition, then i is incremented.
The second i++ (on the right of the RHS) stores the value of i (i.e. now 1) and that value is used in the addition, so 0+1 is assigned to i as a final operation...
The printed value is 1.
Note that the second incrementation has NO EFFECT on the final value of i !!!
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited October 03, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tww2,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Try solving the below example as :
int i=0;
i = (i++)+(i++); // i.e 0(1) + 1(2); Then take the values
without the brackets and perform the addition
System.out.println(i);// The result after taking 0+1 = 1
reply
    Bookmark Topic Watch Topic
  • New Topic