Questions tagged «phantom-reference»


4
了解Java的参考类:SoftReference,WeakReference和PhantomReference
有人可以解释这三个Reference类之间的区别吗(或发布一个很好的解释的链接)?SoftReference> WeakReference> PhantomReference,但当我会用每一个?为什么会有一个,WeakHashMap但没有SoftHashMap或PhantomHashMap? 如果我使用以下代码... WeakReference<String> ref = new WeakReference<String>("Hello!"); if (ref != null) { // ref can get collected at any time... System.gc(); // Let's assume ref gets collected here. System.out.println(ref.get()); // Now what?! } ...怎么了?我是否必须ref在每个语句之前检查是否为null(这是错误的,但是我应该怎么办)?很抱歉急速提问,但我无法理解这些Reference课程...谢谢!
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.