• 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

gc

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following: (paraphrased from a sample on the sun site)
1.public class X {
2.int w = 1;
3.public void m(Object x) {
4. w = 4;
//(edited)
6. x = new Integer(99);
7. Integer y = (Integer)x;
8. y = null;
9. System.out.println("x is" + x);
10. }
11.}
Is it correct to say that y is eligible for garbage collection after line 8. Is it also correct that x is eligible for garbage collection after line 10 (because it is out of scope).
[This message has been edited by Gerry Timmermans (edited December 20, 2000).]
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
y and x are references not objects. new Integer(99) is your object it will be eligible for garbage collection after line 10 when the local variable x is out of scope. I think you are missing a { some where ?

------------------
Warren Bell
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Warren,

When we write Integer y=(Integer)x;
Does that create a new object? If yes, then by writing y=null; Are we making the object availabe for garbage collection?

Please explain. I am totally confused. Thanks in advance.
 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before this line x was an Object class reference. For example:
Object x = new Integer(4); // x is an Object class reference
Integer y = (Integer)x; // cast the reference of x down to y
y and x are references, new Integer(4) is the object that is referenced by a Object class reference x and an Integer class reference y. All classes extend the Object class making the assignment and casts above legal.

------------------
Warren Bell
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic