File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection

 
Vinod N
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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,
Vinod
 
P SOLAIAPPAN
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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

solaiappan
 
anrup kris
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi SOLAIAPPAN
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";
free
 
Terry McKee
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Vinod N'
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Ajith
 
Ragini Kelkar
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic