• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Question from Sybex practice exam

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • 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: 390
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes.Thank you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic