在Java中,垃圾回收器模块会小心地销毁对象,并且对它们没有任何引用的对象都可以进行垃圾回收。以下是有关垃圾回收的一些重要输出问题。
预测以下Java程序的输出:
public class Test
{
public static void main(String[] args) throws InterruptedException
{
String str = new String("GeeksForGeeks");
// making str eligible for gc
str = null;
// calling garbage collector
System.gc();
// waiting for gc to complete
Thread.sleep(1000);
System.out.println("end of main");
}
@Override
protected void finalize()
{
System.out.println("finalize method called");
}
}
输出:
end of main
-
说明:我们知道垃圾回收器会在销毁对象之前调用finalize()方法。但是在这里,技巧是str是String类对象,而不是Test类。因此,