• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Interesting assignment

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
Here is a sample of code
int i = 0;
i=i++;
System.out.println(i);
Can you guess what will be the output ? Output is 0. Could anybody explain the reason !!
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manali,
I have a piece of code similar to the one u have written..
the output is 1 for the following code... wonder why ??
int a,i = 0;
a = i++;
System.out.println(i);
Can anyone explain this ?
Praveen Zala
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by manali:
Hello
Here is a sample of code
int i = 0;
i=i++;
System.out.println(i);
Can you guess what will be the output ? Output is 0. Could anybody explain the reason !!


Hi,
this topic has been discussed around July 15, I just cna not find the link, if u have time, check it.
mick
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manali
Here is what I think the answer is:
The operation i = i++ can be thought of as 2 atomic operations i = i and i = i + 1
When the first operation takes place the value of i = 0 is placed on the stack, when the second operation happens the value of i = 1 is placed on the stack. However in a stack is a LIFO list. So what comes out first is i = 1 and then i = 0. Thus i = 0 overwrites the previous value of i = 1
Hope this explaination helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic