• 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

The truth in garbage collecting???????

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is ... when the variable sHello is elegible for collection?
public class Garbage
{
public static void main(String args[])
{
Garbage gar = new Garbage
System.out.println(gar.sayHello());
}
public String sayHello
{

String sHello = "hellllloo";
sHello = null;
return sHello;
}
}

------------------
Not certified but sooner.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Victor,
First lets start with the basics:
Variables are never eligible for garbage collection!
In java, only objects can be eligible for garbage collection.
This is an important distinction because your question is worded incorrectly. It should read:
When is the object, originally referenced by sHello, eligible for garbage collection?
The answer to that is: immediately after setting the sHello variable to null. Why? Because the original object (string "hellllloo") has no references to it so the JVM makes the string eligible for garbage collection.
Regards,
Manfred.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Victor,
Would you please post the name of the mock exam that included the question.
Thanks.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited July 19, 2001).]
 
Victor Eduardo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made this, but i remember a very similar in this mock exam http://joppa.appliedreasoning.com/javaCert/html/JavaCertification.html
this exam consider, that the object is elegible for collection when the JVM cross the last bracket ( "}" ) in the method.
thats my problem!!
Another guy told me that the object is elegible for collection when the method sends his return...
thanks all.

reply
    Bookmark Topic Watch Topic
  • New Topic