Consider the following code, which objects will be eligible for gc after the return statement? public Object objectTest(Object obj) { Object b = new Object(); Object c, d = new Object(); c = b; obj = null; return c; } My answer is b and d. Any thoughts?
I agree. Though we cannot say anything about about the objects passed as argument and returned by the method. A sentence like "objectTest(new Object());" would leave both eligible for g.c.
Only d is eligible. b was saved to c which is returned to the caller. It may be easier to think in terms of the Object's themselves. You create one Object on line 2 and another on line 3. Which one is eligible for gc?
I think d and obj is answer bcause b is saved to c whic is returned to the caller since the object obj is assigned to NULL it is also ready for garbage collection(though it is a reff for other object passed through the method. Any thoughts? Thanks all
No, obj is not eligible in fact neither b, c, or d are eligible Remember all (Object) variables in Java are references. The concern with gc is about the physical Object which is somewhere in the suburbs (the heap). Read Jose's post just one more time.
Well in that case, "Marc" can't give a correct answer, only the object that Marc references can. Jose's first comment is correct that obj is only eligible if the object being passed is sent as "new Object()" rather than by a reference. The test would make it clearer by asking which objects for sure will become GC eligible, for which obj is not. The object created in line 2 is not eligible because it gains a reference of "c" which was returned. It also has a reference of "b" until "b" goes out of scope when return is called. The object created in line 3 is eligible for GC because "d" is its only reference and d becomes out of scope as soon as return is called. The test generally refers to the objects by the line they were instantiated, not by references. I hope this isn't too confusing. The test would expect you to understand it though.
Originally posted by Marc Peabody: I hope this isn't too confusing. The test would expect you to understand it though.
Sure! So you are an SCJP as is Jose (and me) and so we all passed the test, yet we still differ in our answer! Yes, the wording of the question is confusing. I would even say that this is sort-of a "trick question". Don't get turned off here as I can tell you when I took the test there was only one such question. Nevertheless, the key here is understanding. I really picked up this concept in Peter Hagger's book. (Practical Java Programming)
Ming - There are 3 objects involved: obj - is NOT eligible, it has a reference that existed before the method was called the object referred by b and c is NOT eligible, a reference to it is returned. The object referred to by d is eligible - once the method completes there will be no references to it. This is exactly the kind of question you'll see on the exam, Second point, the line: Object c,d = new Object( ); This is an unusual syntax and it is not currently used on the exam... Does everybody know what this syntax does? :roll: -Bert
Hi Bert, I also thought so at the first. Maybe (just maybe) it is ok to second-guess bartenders eh? Jose, please step up to the plate and play some defense (or clarify yourself).
[ February 28, 2003: Message edited by: Leslie Chaim ]
I dislike the way the question is worded because the result depends on how the method is invoked. I think the exam exhibits more apropiate wording. Another thing bothers me: How long after the execution of return must we consider? we cannot consider the execution of return and to think that we are still within that method. Thus, should we ponder about the sentence in which the method invokation occurs? because if the reference returned by the method is not assigned to a variable, that object would also be eligible for g.c.
Post by:autobot
Friends help you move. Good friends help you move bodies. This tiny ad will help:
a bit of art, as a gift, that will fit in a stocking