Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object for GC....

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic