Assuming go is supposed to be easyMethod it works like this class A { Boolean b; A easyMethod(A a){ a = null; // the reference to a2 was passed in, but is set to null // a2 is not set to null - this copy of a reference is! Return a; // null is returned } public static void main(String args){ A a1 = new A(); // 1 obj A a2 = new A(); // 2 obj A a3 = new A(); // 3 obj a3 = a1. Go(a2); // a3 set to null and flagged for GC - see above for why a1 = null; // so far, a1 and a3 have been set to null and flagged // Some other code } } Two objects are eligible for garbage collection (a1 and a3) be is not because it's only a reference to null.No Boolean was ever made.
Assuming go is supposed to be easyMethod it works like this class A { Boolean b; A easyMethod(A a){ a = null; // the reference to a2 was passed in, but is set to null // a2 is not set to null - this copy of a reference is! Return a; // null is returned } public static void main(String args){ A a1 = new A(); // 1 obj A a2 = new A(); // 2 obj A a3 = new A(); // 3 obj a3 = a1. Go(a2); // a3 set to null and flagged for GC - see above for why a1 = null; // so far, a1 and a3 have been set to null and flagged // Some other code } } Two objects are eligible for garbage collection (a1 and a3).
Be is not because it's only a reference to null. No Boolean was ever made.
And the two objects are a1 and a3 – willcodejavaforfood Jul 21 '10 at 13:30 Ah yes, I only have it my comment. Thanks @wcJff – glowcoder Jul 21 '10 at 13:31 2 were a1 and a3... – Justin K Jul 21 '10 at 13:33 For the sake of correctness I would change "Two objects get garbage collected" to "Two objects are eligible to be garbage collected" – matt be Jul 21 '10 at 13:38 @Justin I lol'd. :) @matt true - my comment reflects this point, but my summary does not.Fixed.
– glowcoder Jul 21 '10 at 13:40.
Provided a1. Go(a2) is actually meant to be a1. EasyMethod(a2), the answer is indeed 2, but not the ones you listed.As Bozho rightly pointed out, be is not initialized, so it doesn't refer to any object.
The two objects eligible for garbage collection at the point of the comment are the ones originally referenced by a1 and a3. A1 is obviously nulled out, and a3 is reassigned to the return value of a1. EasyMethod(a2), which is null.
However, a2 is not affected by the method call, as Java is pass by value, so only a copy of the reference a2 is passed to the method. Even though the copy is set to null, that does not affect the value of the original a2.
1 For mentioning that Java is pass by value. It just never passes Objects, just their references. I understand why it is confusing to some, but I think it's something every (Java) developer should know.
– glowcoder Jul 21 '10 at 13:41 @glowcoder, yeah, I learnt it the hard way, here on SO :-) – Péter Török Jul 21 '10 at 14:19.
First of all the interviewer is wrong about the Boolean -- there is no such object created by this code so there's nothing to be garbage collected. It is incorrect to speak of variables like be and a2 as being garbage collected. Objects are garbage-collected, not variables.
If an in-scope variable references an object, then it cannot be garbage-collected. Simplistically, it's only when an an object is no longer referenced by any variable that it can be garbage collected. So we have three instances of A being created in this code.
They start out referenced by a1 etc. But since variable references change I'll refer to the object instances as A1, A2, and A3. Since you haven't shown a definition of the go method I'm going to assume it's meant to be a call to easyMethod. Since the variable a1 is reassigned to null, nothing is pointing to instance A1, so it can be garbage-collected.
Since the variable a2 is never reassigned (the assignment in easyMethod does not affect the original variable), instance A2 cannot be garbage-collected. Since easyMethod always returns null and a3 is assigned the result of that method, nothing is pointing to instance A3, so it can also be garbage-collected.
For a2's original referant it actually completely depends on what happens in "some other code". If "some other code" doesn't use a2 or a3, then the original a2 object is eligible for garbage collection. That's because the runtime doesn't have to care about lexical scope.It just needs to know that an object can never be referenced again.
Therefore, if "some other code" doesn't utilize a2 or a3, the object they point to can never be referenced again and so is already available for garbage collection.
At the point of // some other code . There is only a2 who can be used. A1 and a3 is null.
– mathk Jul 21 '10 at 14:06 The objects initially referenced by a1 and a3 have no way of being referenced again at the point of "some other code", and so they can be GC'ed. The same can't be said of the object referenced by a2. This doesn't depend at all on later code.
– Justin Ardini Jul 21 '10 at 14:22 @Justin: I never meant to imply otherwise, but I understand the source of your confusion and so have edited it to hopefully reflect better what I was trying to say. – Mark Peters Jul 21 '10 at 14:57 Thanks, much clearer! I had interpreted your phrasing "completely depends" as meaning the code snippet has no effect on whether any of the objects would be GC'ed.
– Justin Ardini Jul 21 '10 at 15:04 1 Yeah, it goes back to the necessary vs sufficient conditions. Not referencing a1/a2/a3 in "some other code" was a sufficient condition for GC eligibility, but certainly not necessary, as indeed a1/a3's original referents were GC eligible either way. "Completely depends" obviously doesn't convey that properly.
– Mark Peters Jul 21 '10 at 15:46.
Because a2 and a3 are not objects. They are variables. Variables are not collectable for the simple reason that they are not allocatable.
1 Snarky, but right. – Justin K Jul 21 '10 at 13:35.
Your question is quite confuse. A garbage collector work on object not on binding. So when you say that a2 is eligible for GC it mean nothing.
You should say the object hold in a2 at line N is eligible for GC at the line N+M. In your example you only have 3 object being instantiate. It is the first create A and the last create A instance that are eligible for GC.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.