• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object for GC....

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic