Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

The truth in garbage collecting???????  RSS feed

 
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.
 
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.
 
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.

 
And inside of my fortune cookie was this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!