package com.qimh.variable.type;
import java.lang.ref.PhantomReference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
public class VariableType {
public static void main(String[] args) {
Object obj = new Object();//强引用
System.out.println("强引用:" + obj);
//软引用
//Object obj2 = new Object();
SoftReference<Object> sf = new SoftReference<Object>(obj);
obj = null;
System.out.println("软引用:" + sf.get());//有时候会返回null
//弱引用
WeakReference<Object> wf = new WeakReference<Object>(obj);
obj = null;
System.out.println("弱引用:" + wf.get());//有时候会返回null
//虚引用
PhantomReference<Object> pf = new PhantomReference<Object>(obj, null);
obj=null;
System.out.println("虚引用:" + pf.get());//永远返回null
pf.isEnqueued();//返回是否从内存中已经删除
}
}
参考链接:https://www.cnblogs.com/yw-ah/p/5830458.html