wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQUEST question on GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQUEST question on GC " Watch "JQUEST question on GC " New topic
Author

JQUEST question on GC

megha gupta
Greenhorn

Joined: Nov 23, 2000
Posts: 3
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
Praveen Zala
Ranch Hand

Joined: Jul 02, 2000
Posts: 118
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
Amond Adams
Ranch Hand

Joined: Nov 28, 2000
Posts: 62
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.
N Mukherjee
Ranch Hand

Joined: Jun 08, 2000
Posts: 141
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


"Knowledge is Power"****************<A HREF="http://www.geocities.com/nityananda_mukherjee" TARGET=_blank rel="nofollow">THREAD/SCJP RESOURCES</A>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JQUEST question on GC