• 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

doubt regarding garbage collection

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void countDown(){
for(int i=0;i>=0;i--)
{
String tmp=Integer.toString(i);//line 5
System.out.println(tmp);
}
System.out.println("Boom");//line 8
}
when program reaches line 8 how many objects created in line 5 are eligible for garbage collection?
i thought it was 11 but the answer is 10 why?
tmp is a local variable so it should be garbage collected isnt it?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you start at i=10 (rather than the typo i=0), I would say the answer is 11.

Where did this question come from?
 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is from whizlab mockexams practice exam 4
 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya sorry i typed it wrong its
for(i=10;i>=0;i--)
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MMmmm, I think it is 11... because after line 8 all the objects (10 to 0, it means 11) lose the reference variable (also the last one, because it is declared inside the for loop)... only if you declare the reference variable outside the loop, then it'll be 11... or if you remove the >= and only put >...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic