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
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"
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?
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
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.
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 ...
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.
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.