| Author |
question on GC frm go4java mock test
|
Ram Murthy
Ranch Hand
Joined: Aug 02, 2005
Posts: 91
|
|
After which line the object initially refered by str is eligible for garbage collection?Select one correct answer. 1 class Garbage 2 { 3 public static void main(String arg[]) 4 { 5 String str=new String("Hello"); 6 String str1=str; 7 str=new String("Hi"); 8 str1=new String("Hello Again"); 9 return; 10 } 11 } My answer for this is "After line no. 8 ". Is this correct ?? If not please say what the correct answer is and the explanation. I was searching thru the post to see if this question was already asked, but couldn't find one. In the process of searching found some articles on String and it is different than other object with respect to GC, can anyone explain it again.. Thanks, JP
|
Cheers,
Ram
|
 |
Swosti Dipan Pal
Ranch Hand
Joined: Sep 23, 2005
Posts: 70
|
|
I too think the same way you have thought !!! Cheers, -Biswa
|
Thanks<br />-Swosti
|
 |
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
Hi, You are right... At line 6 ///there are two references for that object... //line 7 only reference remains that of str1 //after line 8 ..even this points to another location... So no more references exists for the object and eligible for GC..
|
 |
Akhilesh Trivedi
Ranch Hand
Joined: Jun 22, 2005
Posts: 1493
|
|
I go for 8.
|
Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
|
 |
sampath garapati
Ranch Hand
Joined: Sep 26, 2005
Posts: 39
|
|
I think that After line 7 the initially referred object by str is eligible for GC. I am not sure. All of you were said it is after 8. Please explain. Sampath
|
 |
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
From the above post..
At line 6 ///there are two references for that object... str and str1 are pointing to the same object So 2 references remain //line 7 only reference remains that of str1 Here str is now pointing to a different and new String object and str1 is still pointing to the old object.. So only 1 reference remains //after line 8 ..even this points to another location. Here str1 is pointing to a new string object...and the old object now has no reference..So 0 reference... Hope you get it now...
|
 |
 |
|
|
subject: question on GC frm go4java mock test
|
|
|