Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Victor Eduardo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • 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.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic