aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC doubt! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC doubt!" Watch "GC doubt!" New topic
Author

GC doubt!

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Assigning null to a reference causes the object to become eligible for garbage collection.


TRUE/FALSE ?



Help!

Thanks,
cmbhatt


cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Could you possibly write a program to prove or disprove that statement?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
I'd say it is false.

Objects become eligible for GC only when nothing points TO them, i.e. the references to them have been set to null.
But if an object in initialized, then put into an array and then the object is set to null, the array still uses it/references to it.

What do you think?


Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
Omer Haderi
Ranch Hand

Joined: Sep 27, 2006
Posts: 42
The question is too abstract to answer, we must have take assumptions in order to answer.

case 1. if an object is referred by to references then it is FALSE
ex:


case 2. if an object is referred by one reference then it is TRUE


case 3. if a reference is directly assigned to null, then it's not TRUE neither FALSE. There is no object.


I hope in the exam this kind of questions contains code also, otherwise
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

1.Assigning null to a reference causes the object to become eligible for garbage collection.

Ans:This question as it is ambiguous. If you are forced to select between true and false then go for false.

2.Assigning null to a reference always causes the object to become eligible for garbage collection.

Ans: Definite False

3.Assigning null to a reference can cause the object to become eligible for garbage collection.

Ans: Definite True.

Thats my view point.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Omer and Srini for quick and helpful reply,

Srini, I completely agree with what three statements you jotted.
The first one is very ambiguous :roll: and definitely, saying it false will do the favor as I think too.

Next two, support you to decide quickly to answer.



I hope in the exam this kind of questions contains code also, otherwise

Tommaso, I hope this too!


Thanks,
cmbhatt
John Mathew
Greenhorn

Joined: Apr 17, 2007
Posts: 13
37. Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
Originally posted by John Mathew:
37. Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.


to a)
-> No, you can only suggest JVM to run the gc.

to b)
-> No, it is not guaranteed that the finalize() method is called. (K&B, SCJP 5, page 253)

to c)
-> I am not sure here, but I think this is correct, as finalize() is declared in the objet() class. Thinking twice it must be, because everything up the inheritance tree must be cleaned. It is vice versa of the constructor logic.

to d)
-> definitely not. You never know when it is going to run and what it going to do, besides cololecting garbage.

Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC doubt!