当前位置: 技术问答>java相关
那位高手为我解释一下
来源: 互联网 发布时间:2014-12-31
本文导语: When will the object created as myObject become eligible for deletion? class example { public static void main( String args[] ) { UseObject(); } private void UseObject() { String anObject = AllocateObject(); System.out.println( anObject ); ...
When will the object created as myObject become eligible for deletion? class example
{
public static void main( String args[] )
{
UseObject();
}
private void UseObject()
{
String anObject = AllocateObject();
System.out.println( anObject );
}
private String AllocateObject()
{
String myObject = new String( "When will I be deleted?" );
return myObject;
}
};
A. When the AllocateObject() function completes
B. When the call to System.out.println() completes
C. When the UseObject() function completes
D. When the main program completes
我认为 myObject 在AllocateObject 中产生,AllocateObject 结束了,myObject就没有用了,所以我选A, 可是答案是 C ,烦劳高手为我解释一下
{
public static void main( String args[] )
{
UseObject();
}
private void UseObject()
{
String anObject = AllocateObject();
System.out.println( anObject );
}
private String AllocateObject()
{
String myObject = new String( "When will I be deleted?" );
return myObject;
}
};
A. When the AllocateObject() function completes
B. When the call to System.out.println() completes
C. When the UseObject() function completes
D. When the main program completes
我认为 myObject 在AllocateObject 中产生,AllocateObject 结束了,myObject就没有用了,所以我选A, 可是答案是 C ,烦劳高手为我解释一下
|
Maybe you misunderstand what is myObject.
In java, everything is an object which you should new it if you want it (except those of primitive type, such as integer...).
In your code, myObject and anObject are not string object but are two string object reference pointing to a same string object you created in you AllocateObject() method.
So, when you quit AllocateObject, the released is not an string object created from heap, but a reference variable allocated from stack.
Since your anObject reference keep reference to this string object, It can not be collected by garbage collector untill anObject variable is released.
Furthmore, It doesn't mean this string object has been collected if nobody reference it. The collection depends on the resource usage. and garbage collector do it in the lazy collection way.
In java, everything is an object which you should new it if you want it (except those of primitive type, such as integer...).
In your code, myObject and anObject are not string object but are two string object reference pointing to a same string object you created in you AllocateObject() method.
So, when you quit AllocateObject, the released is not an string object created from heap, but a reference variable allocated from stack.
Since your anObject reference keep reference to this string object, It can not be collected by garbage collector untill anObject variable is released.
Furthmore, It doesn't mean this string object has been collected if nobody reference it. The collection depends on the resource usage. and garbage collector do it in the lazy collection way.