• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question from Sybex practice exam

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer is Immediately after line 10.

But shouldn't the answer be after line 4? Because temp is local variable and local variables go out of scope after method exits ,becoming eligible for garbage collection.

Thanks,
Veena
 
Ranch Hand
Posts: 499
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Veena Pointi wrote:
Answer is Immediately after line 10.

But shouldn't the answer be after line 4?


The answer seems correct to me.

Veena Pointi wrote:
Because temp is local variable and local variables go out of scope after method exits
Veena


Indeed

Veena Pointi wrote:
becoming eligible for garbage collection.

Thanks,
Veena

This happens immediately after line 10 for the String object instantiated on line 3 because there will no longer be any references to it then.  Specifically, at the aforementioned method exit, the String reference is returned, assigned to the result String reference, printed, and after line 10 since the code no longer references  the String object instantiated on line 3, then the String object instantiated on line 3 becomes eligible for garbage collection.

Does that help?
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.Thank you.
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic