rxin2009 2012-11-01 11:21
浏览 321
已采纳

华为面试题

[code="java"]
public class ReferenceTest {
public static void main(String[] args) {
Obj o=new Obj("weak");
WeakReference weak=new WeakReference(o);
o=null;
System.gc();
System.out.println(weak.get());
}
}

class Obj{

private final String name;

Obj(String name){
    this.name=name;
}

@Override
protected void finalize() throws Throwable {
    System.out.println("执行finalize方法:"+name);
    super.finalize();
}

@Override
public String toString() {
    return name;
}

}
[/code]

上述代码的输出结果是什么?为什么?

  • 写回答

2条回答 默认 最新

  • qq415241704 2012-11-01 11:36
    关注

    null
    执行finalize方法:weak 因为WeakReference是弱引用,而弱引用是会在对象使用完后马上被回收掉的,所以weak.get()为null

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?