• 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

i = i++; .....?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Look at the code below,
public static void main(String args[])
{
int i =0;
i = i++;
System.out.println(i);
}
It is printing 0 , but i was expecting 1.
Will anyone pls explain me..?
--------------
Prashant
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
That is bcoz ... i++ means.. for the execution in current line the value of i will be still 0.But as control goes to next line., next time i is accessed for any operation, its value will be changed.. So it becomes 1. If instead of i++ if u use ++i .. in the execution of i = ++i , i value will be incremented n new value of i i.e 1 is assigned to i.So o/p 'll be 1.
Above stmt is justifed better with following code..
public static void main(String args[])
{
int i =0;
i = i++;
System.out.println(i);
i = ++i;
i = i++;
System.out.println(i);
}
Hope stmt is justified
###################################################
WISH U ALL HAPPY NEW YEAR 2003
HAKUNAMATATA !!!

[ January 01, 2003: Message edited by: Chetan Mishra ]
[ January 01, 2003: Message edited by: Chetan Mishra ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The i++ operand evaluates i to 1 but reutrn 0. Since the ++ operand takes precedence before the assignment, 0 assigns to variable i at last.
Hope this helps.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's kinda like when you ket a cut on your elbow and you go up to your mom and say "Mom -- it hurts when I move like this..." and your mom says -- "So, don't move like that."
The moral of this story is: so don't do that in your code -- its confusing to read and just plain silly.
 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats rt Peter.But still i have doubt.....
1. int i=1,j=1;
2. i=j++;
3.
At line 2 , fallowing things will happen,
a) Coz of hiegher precedence of ++ ,it is applied to j variable. rt?
b)next, assignment takes place.
If this is the case,Is it that there are two copies of j.'i' is pointing to first copy and its assigned only after another copy of j gets incremented.
 
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
Maha's technique for solving ++ and -- related problems
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how about this...

why does i become 3?
[ January 02, 2003: Message edited by: Aaron Anders ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After i=i++, i is 0.
After i=++i, i is 1.
Comparison is false so do bit after the ||
After i++, i is 2
After ++i, i is 3.
Second comparison is 1 == 3, which gives false
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic