This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
Evil is afoot. But this tiny ad is just an ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic