This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object for GC.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object for GC...." Watch "Object for GC...." New topic
Author

Object for GC....

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
source:www.javabeat.net



At which point will the word "Harpic" be printed out when this code is executied?

(1) After executing the line after the comment //one
(2) After executing the line after the comment //two
(3) After executing the line after the comment //three
(4) It is impossible to say, the String "Harpic" may not be output at all

my answer is 1

but answer is 4

can anyone elaborate this?

[ December 16, 2008: Message edited by: Ganeshkumar cheekati ]

SCJP5 and SCWCD1.5
Think Twice Act Wise...
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

One object is elligible for GC.
That is after the LINE1 ( As you thought.)
But the statement system.gc(); is not explictly written.
So GC may or may not happen.
Look at the option 4.
It is impossible to say, the String "Harpic" may not be output at all
[ December 16, 2008: Message edited by: James Tharakan ]

SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
See Harpic is printed in finalize() method. And JVM says it will run finalize atmost one. Here atmost means zero or one time. It is possible that the finalize method will never be called.


SCJP 6
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Ganeshkumar,

here is my view..

public void oui()
{
har = new Harpic();
mno(har);//here we are passing the copy of har-one
har=null;//finally original har is made null- four.
}
public void mno(Harpic har)
{
Harpic pic=har;//here assingning the copr of har to pic
pic=null;//making pic null so -two
har=null;//here making the har null(which is the copy of har in oui())-three
}}

if "harpic" has to be printed out then the oringinal har would be consider as eligible for GC and run once, but it's not gaurenteed.

Preetha
[ December 16, 2008: Message edited by: Preetha Arun ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Object for GC....
 
Similar Threads
Garbage Collection Question from Devaka's Exam Labs for SCJP6 - diagnostic exam question number 70.
doubt in continue and break
Question on garbage collection from mock exam
Flow control
Some thoughts for the day!