• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Garbage collection

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'Vinod N'
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Ajith
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic