这里记录一些java中关于引用的知识。
java中的引用
一、java中关于引用的测试
public class ReferenceTest {
public static void main(String[] args) {
SoftReference<Person> softReference = new SoftReference<Person>(new Person("huhx", "123456"));
System.out.println(softReference.get()); // username: huhx, password: 123456
System.gc();
System.runFinalization();
System.out.println(softReference.get()); // username: huhx, password: 123456 WeakReference<Person> weakReference = new WeakReference<Person>(new Person("linux", "12345"));
System.out.println(weakReference.get()); // username: linux, password: 12345
System.gc();
System.runFinalization();
System.out.println(weakReference.get()); // null ReferenceQueue<String> refQueue = new ReferenceQueue<String>();
PhantomReference<String> referent = new PhantomReference<String>(new String("T"), refQueue);
System.out.println(referent.get());// null
System.gc();
System.runFinalization();
System.out.println(refQueue.poll() == referent); // true
}
}
关于java引用的详细说明,可以参考博客:http://www.cnblogs.com/rollenholt/archive/2011/09/21/2183077.html