File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic

Garbage collection

Vinod N

Joined: Oct 06, 2000
Posts: 2
I've a doubt regarding a question from a sample test.
1)How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4
I thought the answer is c)2 the String "Frieda" will GC'ed as the ref has been set to null as well as the String "Nick" which is lying in the pool without a reference.
But the answer is given as b. Could some one please explian whether I'm missing something here.

Another question:
The Container methods add( Component comp ) and add( String name, Component comp ) will throw an IllegalArgumentException if comp is a:
a) button
b) list
c) window
d) textarea
e) container that contains this container

Thanks in advance,
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
u r first question
1. String name;
2. String newName = "Nick";
3. newName = "Jason";
4. name = "Frieda";
5. String newestName = name;
6. name = null;
7. //Line A
In line 3 , "Nick" is eligible for GC
In line 4, String name="Frieda"; //initialisation
In line 5, the String references newestname, name
will point to "Frieda"
In Line 6, name is not pointing anywhere

Note: "Frieda" is still pointed by newestName

Here only u mis calculated GC as 2,
in fact it is one only i.e "Nick" and not "Frieda"

I hope u r clear

anrup kris

Joined: Oct 22, 2000
Posts: 17
For your 2nd question,I thought the answer was Window and container that contains this container
Window component has to be stand alone on the desktop,and cant be contained within another container
I think the second answer is pretty clear.
anyone has a better idea?
free hoo

Joined: Oct 23, 2000
Posts: 4
i had doubt
i understood that Strings are immutable then how can you assing a new value to
newName .
please explain iam not clear with the concept.
2.String newName = "Nick";
3. newName = "Jason";
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 175
String name; (String declared. Nothing as it pertains to this topic happens.)
String newName = "Nick"; (newName points to an area where the new string "Nick" is located.
newName = "Jason"; (newName points to an area where the new string "Jason" is located. It is important to realize at this point that poor "Nick" from the above line has any references, no one is pointing to it. So the string "Nick" is eligible for garbage collection.)
name = "Frieda"; (name now points to an area where the new string "Frieda" is located.)
String newestName = name; (newestName now points to an area where the already created String "Frieda" is located. "Frieda" now has TWO references pointing to it.)
name = null; (name was pointing at "Frieda". Now it isn't. But "Frieda" doesn't feel too bad because newestName is still pointing to it. -There is still a reference to the string "Frieda".)
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4
In this way only one String is eligible for garbage collection.
Suren Babu

Joined: Oct 23, 2000
Posts: 27
Strings are Immutable only. I will tell you what happens exactly below...

2.String newName = "Nick"; String "Nick" is created (if not in the compiler pool) and String reference newName points to that.
3. newName = "Jason"; Here another String "Jason" is created (if not in the compiler pool) and now String reference newName points to that. Here it is not the already existing String "Nick" is modified to "Jason". what happens a new String is created (jason) and is assigned to newName.
Hope this helps ...
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
'Vinod N'
Read this post for more details.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Ragini Kelkar

Joined: Oct 19, 2000
Posts: 9
Shouldn't the answer be :
0 objects eligible for gc as all these are created in the String pool?
If they were created with new then they would be created on the heap and hence eligible for GC ?
Edy, Yu

Joined: Oct 20, 2000
Posts: 3
I just wanna add a note that name is not a object. It's just reference to an object. There are threee objects created: "Nick", "Jason" and "Frieda". All of them are in the pool somewhere in JVM. Only "Nick" is illegable to be GCed cus no reference is on it.
Kourosh Keshavarzi
Ranch Hand

Joined: Oct 23, 2000
Posts: 66
This message is regarding anrups comments. I think you've answered a non related question of mine. I was having a problem trying to add a frame to a frame. So this is illegal since frame is a subset of window and windows have to be stand alone as you were saying.
I agree. Here's the link:
subject: Garbage collection
It's not a secret anymore!