This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
i have a doubt in this question..please explain.. Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here public class BettyAck { public static void main(String argv[]){ BettyAck b =new BettyAck(); } public BettyAck() { Integer x = new Integer(10); findOut(x); Integer y = new Integer(99); Integer z = y; z = null; findOut(y); //here } public void findOut(Integer y){ y = null;
}
a)0 b)1 c)2 d)3 e)4 i think the answer is 2. 1.y=null means (the reference y and x points nothing or the value of the object 10 becomes null)i think the first one is correct 2.can i say only two objects are created in the above code...(the space allocated only for "10" and "99") (x,y,z are references)
Answer is 0.Because even the objects are assigned to null , still the thread refering those objects.Thread refer those objects untill the end of scope.
First see how many times 'new' is being called. Here three times for objects b,x and y. In the method call findOut(), you are only passing a copy of the reference to the object. Setting this reference to null will not affect the original object back in the calling method.
Similarly, setting z= null, will not affect the original value of y. z was pointing to y and now it is pointing to nothing.
Therefore, none of the objects are eligible for gc.
it depends on what you mean by "the end o the method findOut". The original question said "how many at the comment?". since the comment is still inside the method (we have not yet hit the closing curly brace), the objects created in the method are NOT yet eligible. but if by "the end of the method" you mean "after leaving the method", then i agree there are two. of course, after you've left the method, the next thing you do is leave main, so the BettyAck object also becomes available.
Never ascribe to malice that which can be adequately explained by stupidity.
In java when when a method call happens, that has an Object as a parameter, a copy of , reference to that object, is passed to the method.Therefore in the method if y = null is done, the object still has a reference that is outside the method, and therefore it cannt be garbage collected
See, when you pass an object reference to a methos, actually you are passing a copy of the reference. though you make it null in the called method, still the object is being reference by the original reference varialble!!!
so, in this sityaltion, No object is eligible for the garbage collection. refer the garbage collection tutorial in this website. thanks®ards krishna scjp1.4 scwcd1.4