起航学习网

- 让每个人都能学到最前沿新知识、新技能!
起航学习网
当前位置: 起航学习网 > 短期培训 > 编程语言 > 一文看懂Java引用类型有哪些

一文看懂Java引用类型有哪些

时间:2022-06-08 14:17:30来源:IT培训网 作者:Java学习网 已有: 名学员访问该课程

  快捷搜索:java引用类型有哪些

前言: 在 Java 中,有四种类型的引用在它们被垃圾收集的方式上有所不同。 1.强引用 2.弱引用 3.软引用 4.幻影参考 强引用:

在 Java 中,有四种类型的引用在它们被垃圾收集的方式上有所不同。

1.强引用

2.弱引用

3.软引用

4.幻影参考

强引用:

这是引用对象的默认类型/类。任何具有活动强引用的对象都没有资格进行垃圾回收。只有当被强引用的变量指向 null 时,对象才会被垃圾回收。

MyClass obj = new MyClass();          

这里的 'obj' 对象是对新创建的 MyClass 实例的强引用,当前 obj 是活动对象,因此不能被垃圾收集。

对象=空;
//'obj' 对象不再引用实例。
所以'MyClass 类型对象现在可用于垃圾收集。
// Java program to illustrate Strong reference
class Gfg
{
	//Code..
}
public class Example
{
	public static void main(String[] args)
	{
		//Strong Reference - by default
		Gfg g = new Gfg();			
		//Now, object to which 'g' was pointing earlier is
		//eligible for garbage collection.
		g = null;
	}
}

弱引用:

弱引用对象不是引用对象的默认类型/类,在使用它们时应明确指定。

这种类型的引用在 WeakHashMap 中用于引用条目对象。

如果 JVM 检测到一个只有弱引用的对象(即没有强或软引用链接到任何对象对象),该对象将被标记为垃圾回收。

要创建此类引用,使用java.lang.ref.WeakReference类。

这些引用在实时应用程序中使用,同时建立一个 DBConnection,当使用数据库的应用程序关闭时,垃圾收集器可能会清理该 DBConnection。

//Java Code to illustrate Weak reference
import java.lang.ref.WeakReference;
class Gfg
{
	//code
	public void x()
	{
		System.out.println("GeeksforGeeks");
	}
}
public class Example
{
	public static void main(String[] args)
	{
		// Strong Reference
		Gfg g = new Gfg();	
		g.x();		
		// Creating Weak Reference to Gfg-type object to which 'g'
		// is also pointing.
		WeakReference<Gfg> weakref = new WeakReference<Gfg>(g);		
		//Now, Gfg-type object to which 'g' was pointing earlier
		//is available for garbage collection.
		//But, it will be garbage collected only when JVM needs memory.
		g = null;		
		// You can retrieve back the object which
		// has been weakly referenced.
		// It successfully calls the method.
		g = weakref.get();		
		g.x();
	}
}

输出:

GeeksforGeeks
GeeksforGeeks

可以招募两种不同级别的弱点:软弱点和幻影弱点

软引用:

在软引用中,即使对象可以进行垃圾回收,也不会被垃圾回收,直到 JVM 严重需要内存。当 JVM 内存不足时,对象会从内存中清除。创建这样的引用使用java.lang.ref.SoftReference类。

//Code to illustrate Soft reference
import java.lang.ref.SoftReference;
class Gfg
{
	//code..
	public void x()
	{
		System.out.println("GeeksforGeeks");
	}
}
public class Example
{
	public static void main(String[] args)
	{
		// Strong Reference
		Gfg g = new Gfg();	
		g.x();		
		// Creating Soft Reference to Gfg-type object to which 'g'
		// is also pointing.
		SoftReference<Gfg> softref = new SoftReference<Gfg>(g);		
		// Now, Gfg-type object to which 'g' was pointing
		// earlier is available for garbage collection.
		g = null;		
		// You can retrieve back the object which
		// has been weakly referenced.
		// It successfully calls the method.
		g = softref.get();		
		g.x();
	}
}

输出:

GeeksforGeeks
GeeksforGeeks

幻影引用:

幻影引用所引用的对象符合垃圾回收条件。但是,在将它们从内存中删除之前,JVM 会将它们放入一个名为 'reference queue' 的队列中。在对它们调用 finalize() 方法后将它们放入引用队列中。使用java.lang.ref.PhantomReference类创建此类引用。

//Code to illustrate Phantom reference
import java.lang.ref.*;
class Gfg
{
	//code
	public void x()
	{
		System.out.println("GeeksforGeeks");
	}
}
public class Example
{
	public static void main(String[] args)
	{
		//Strong Reference
		Gfg g = new Gfg();	
		g.x();		
		//Creating reference queue
		ReferenceQueue<Gfg> refQueue = new ReferenceQueue<Gfg>();
		//Creating Phantom Reference to Gfg-type object to which 'g'
		//is also pointing.
		PhantomReference<Gfg> phantomRef = null;
		phantomRef = new PhantomReference<Gfg>(g,refQueue);		
		//Now, Gfg-type object to which 'g' was pointing
		//earlier is available for garbage collection.
		//But, this object is kept in 'refQueue' before
		//removing it from the memory.
		g = null;		
		//It always returns null.
		g = phantomRef.get();		
		//It shows NullPointerException.
		g.x();
	}
}

运行时错误:

线程“主”java.lang.NullPointerException 中的异常
    在 Example.main(Example.java:31)

输出:

GeeksforGeeks

 

文章出自:http://qh.itpxw.cn/peixun/software/2022121750.html

文章标题:一文看懂Java引用类型有哪些



免责声明:本站文章均由入驻起航学习网的会员所发或者网络转载,所述观点仅代表作者本人,不代表起航学习网立场。如有侵权或者其他问题,请联系举报,必删。侵权投诉

你也许会喜欢如下的文章?
(责任编辑:深圳学历教育网)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
培训学校
IT培训网 访问该机构站点 报名留言 加为好友 用户等级:注册会员 用户级别:10 机构名称:IT培训网 联 系 人:罗老师 联系电话:13783581536 联系手机:13783581536 在线客服:起航学习网客服 在 线 QQ:起航学习网客服 电子邮件: 网站域名:http://www.itpxw.cn 注册时间:2016-07-18 11:07 最后登录:2024-02-20 13:02
推荐内容