Java从WeakReference实例的get()方法创建另一个强引用实例吗?

huangapple go评论58阅读模式
英文:

Does Java create another strong reference instance from get() of WeakReference instance

问题

例如,

WeakReference<Obj> weakObj = new WeakReference<>(obj);
Obj obj2 = weakObj.get();

在这个例子中,obj2 是强引用类型吗?

如果是的话,是否意味着只有在其父引用被垃圾回收后,GC 才能够移除这个引用?

英文:

For example,

WeakReference&lt;Obj&gt; weakObj = new WeakReference&lt;&gt;(obj);
Obj obj2 = weakObj.get()

In this example obj2 is strong reference type?

If yes means GC is not able to remove this reference until its parent get GCed?

答案1

得分: 2

是的

默认引用是强引用

您可以在这个其他问题中找到更多信息

英文:

Yes

Default references are strong references

You have some more information in this other question

答案2

得分: 2

你的 weakObj 是一个 WeakReference,是的,get() 方法会返回一个标准引用。

英文:

Your weakObj is a WeakReference, and yes, get() returns a standard reference.

huangapple
  • 本文由 发表于 2020年9月28日 16:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64098912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定