This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question - please verify my answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question - please verify my answer" Watch "Thread question - please verify my answer" New topic

Thread question - please verify my answer

Ming Tan

Joined: Jan 09, 2003
Posts: 23
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?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
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?

Normal is in the eye of the beholder
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
There are only two objects created using new. One of them is getting returned. Only the other one is eligible for GC.
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
I also thought so, but I think Jose is right. The key is 'eligible' and based on the snippet b and d are certainly 'eligible'.
Any thoughts?
Md.Afzalur Rashid
Ranch Hand

Joined: Oct 29, 2002
Posts: 47
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

Be Confident,be trusty,Count your age by friends not by years
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
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.
Marc Peabody
pie sneak

Joined: Feb 05, 2003
Posts: 4727

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.

A good workman is known by his tools.
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Originally posted by Marc Peabody:
I hope this isn't too confusing. The test would expect you to understand it though.

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)
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
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:

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
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 ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.
I agree. Here's the link:
subject: Thread question - please verify my answer
jQuery in Action, 3rd edition