This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Another Exercise regarding Garbage Collection: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Another Exercise regarding Garbage Collection:" Watch "Another Exercise regarding Garbage Collection:" New topic
Author

Another Exercise regarding Garbage Collection:

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Float f = new Float(3.5f);
Float f = new Float(3.0f);
f = null;
f= null;
When is object referenced f eligable for g.c?
How many objects are eligable for g.c.
I think there are 2 objects eligable for g.c.
First the Float(3.5f) and then the last reference
Of f which is Float(3.0f).
Is that correct? What is the earliest point when
f is eligable for g.c. is it line 3?
Appreciate your answers.
Thomas
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Do you mean to name your references as f1 and f2... you have them both named as f???


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
The references are both "f" in this question.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Float f = new Float(3.5f);
Float f = new Float(3.0f);
f = null;
f= null;

Ok, just trying to make sure I understand. Since you can't declare the same variable twice, I think you mean...

Is that correct? If so, then I believe your first object (3.5) is eligible for gc at line 4, and your second object (3.0) is eligible at line 5.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The references are both "f" in this question.
As Blake implied, such a program fragment would produce a compile-time error.
Where did you find this example?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another Exercise regarding Garbage Collection:
 
Similar Threads
Error with JQPlus Question
About Garbage Collection
Garbage Collection of Strings and Objects
String literal
How many Objects are garbage collected?