hi Consider the following code: 1. public void method(String s){ 2. String a,b; 3. a = new String("Hello"); 4. b = new String("Goodbye"); 5. System.out.println(a + b); 6. a = null; 7. a = b; 8. System.out.println(a + b); 9. }
Where is it possible that the garbage collector will run the first time?
A.
Just before line 5 B.
Just before line 6 C.
Just before line 7 D.
Just before line 8 E.
Never in this method please explain thank you all sherin
I have a doubt on this: at line 5: a + b would actually be creating a new String object "HelloGoodbye" - is this correct. If yes, after line 5, this string "HelloGoodbye" can be GCed isn't it?
Can someone please clarify this? Thanks a million -sampaths77
Hi there, The answer should be C) Just before Line 7 because it after line six i.e when 'a' is assigned the null value, is object which was referencd by 'a' eligible for GC as it does not hold any references. It doesn't make a diff if 'a' is later assigned an object referenced by 'b'. The basic point is that an object is eligible for GC if there are no active references for the same. thus my ans is C
I tend to agree with sampath on this question. a+b in line 5 produces a new object which is not being referred to be any varable after the execution of line 5. So, my answer would be b - just before line 6. But I have a doubt. Is the question valid? I mean, since there is no gaurantee about gc, how can u say that gc will run at given point? Could someone please explain?
hi my answer is just before 7. because after "a+b" it is anonymous string literal so it will not be GCied. if i were wrong please rectify. thanks ---shi
I remember reading somewhere that the reference to a in the System.out.println(a+b); statement would keep the garbage collector from marking it until the end of the method... If anyone recalls the same thing or if I'm wrong, I'd like to hear about it. Thanks.
Hi, My answer is C b'coz when we asign null value to an object then we loose the reference to that object.As soon as we loose reference then that object will becomes the candidate for gc.so i think the answer is C. greddy
Folks, Refer to William Brogden's explanation in this page: http://www.javaranch.com/ubb/Forum24/HTML/004079.html The temporary string created in line 5 will be available for gc. So, answer is B. [ Ajith corrected the URL ] [This message has been edited by Ajith Kallambella (edited September 15, 2000).]
Hi all I totally agree with srikrish as i too have seen the same Q and A some where. i dont remember.. Well once the println prints the value of a+b ie hellogoodbye the gc is collected. Cheers Venu
OK Guys, from William Brodgen explanation to another similar question, I reason it out as follows: an unnamed object String object a+b will be created and will be eligible for GC after line 5 executes. Therefore, my answer would B. Is everybody ready to agree with this as the answer?? -sampaths77
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards