1public class Q2 { 2 public static void main (String[] args) { 3 String s = "Hello World "; 4 String s1 = "Hello"; 5 s = s1; 6 s = null; 7 s1 = null; 8 System.out.println(s); 9 } 10} How many objects will be Garbage collected by the time execution completes Line 8? 1One 2Two 3Three 4Cannot determine the answer is 4,I don't know why??? who can tell me?
AnthonyHan
Greenhorn
Joined: Apr 06, 2001
Posts: 4
posted
0
I will attend the exam tomorrow,who can explain it for me?Thanks a lot!!!
Siobhan Murphy
Ranch Hand
Joined: Oct 19, 2000
Posts: 72
posted
0
It is not possible to determine when garbage collection will take place, only to say that a piece of unused memory has become eligible for collection. Good Luck in the exam.
Nandini Gangopadhyay
Ranch Hand
Joined: Apr 02, 2001
Posts: 79
posted
0
Anthony, Read the question, it says "will be garbage collected" and not "how many are eligible for garbage collection". Good luck for ur exam, tell us how u did Nandini
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Anthony, Just to add to Nandini's answer; there are no guarantees about when the garbage collector will run or the order in which objects eligible for collection will be collected. If the program is very small it may exit before the gc runs at all.
Hope that helps.
------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
Yeah read the question carefull before answering it.Of course u cannot determine when will be the object will be collected by Garbage Collection but u can determine when the object is eligible for garbage collection.Yes sometimes ur program will end before any garbage collection takes place hence giving no chance to run the thread garbage collector. For exam i will say read teh question carefully before naswering. Good Luck ------------------