Dear Friends,
I would like to add some more concrete details to the answer.
1) The object is sent to gc only when all the references are removed for that object.
2)If two or more reference variables refer to the same object.And if any one is set to null,that does not mean that the object is set to null and sent to gc, rather the object reference is removed for that variable.
i.e if x and y refer to the object Integer, and later if u set y=null, then the object reference of y is removed not the object.
You can see this by running this example.
public class SimpleRef
{
public void getValue(Object x)
{
x=new StringBuffer("Hello");
StringBuffer y=(StringBuffer)x;
y.append("Sun"); //later comment this
//y=null; //later uncomment this & see the result.
System.out.println("The value of x =" + x);
System.out.println("The value of y =" + y);
}
public static void main(
String[] args)
{
SimpleRef myRef=new SimpleRef();
StringBuffer c=new StringBuffer("from main");
System.out.println("Entering into prog");
myRef.getValue(c);
}
}
Prasad.
------------------
[This message has been edited by Prasad Ballari (edited October 24, 2000).]