• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JQUEST question on GC

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a method, which creates a number of String objects in the course of printing a count down sequence.
1. public void countDown() {
2. for( int i = 10 ; i >= 0 ; i-- ){
3. String tmp = Integer.toString( i );
4. System.out.println( tmp );
5. }
6. System.out.println("BOOM!");
7. }
When the program reaches line 6, how many of the String objects created in line 3 are eligible for garbage collection? Assume that the System.out object is not keeping a reference.
The solution is none , but shudn't it be 9.
Everytime a new string object is assigned to temp and the previous one is dereferenced ?
TIA
Megha
 
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,
Actually it should be NONE itself (which is true)....
My reasoning goes like this - everytime the JVM comes into
picture when a new object is assigned ... before which an old
object is dereferenced ...
Prav
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All objects ever assigned to tmp, will be elligible for gc.
Why?
String tmp is declared and only accessable in the "for" loop. Once you move ahead of that loop you loose all accessability (which is the major rule for garbage collection that all inaccessable objects may be marked for gc) Therefore they are certainly eligible for gc.
Now as for the given mock answere there are only two possibilities;
1) Integer.toString(int i) returns a String from String pool, which it does'nt, it explicitly creates a new String(...) and returns its reference while not at all keeping one for itself.
2) Given mock exam answere is wrong!!!
You decide and let me know in any case.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
This question has been debated since time immemorial.
I think there are two school of thoughts.
The answer is :11 and please see the discussion after you search
with "boom" keyword:-)
BTW, this question deals with the intern() pool of dealing with
String objects..and is outside the syllabus of SCJP2.
Regds.,
NM
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic