gpt4 book ai didi

java - Java 是否隐式定义了对类中使用的对象的引用?

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:28 25 4
gpt4 key购买 nike

对于Java中的引用类型,看了书,上网,我还是有些疑惑(或者我理解错了这个概念)。
如果有人消除我的疑虑,那将对我有很大帮助。

我举一个类的例子,其中包含类变量、实例变量和局部变量。

public class Test {
public static ArrayList<String> listCommon = new ArrayList<String>();
private HashMap<String, String> mapInstance;

public Test() {
mapInstance = new HashMap<String, String>();
}

public void doSomething(String key) {
ArrayList<String> local = new ArrayList<String>();
if(key != null){
local.add(mapInstance.get(key));
}

System.out.println("Value is added in instance Map: ", mapInstance.get(key));
}
}

我的问题是;
1. listCommon(静态变量)和mapInstance(实例变量)是对垃圾收集器的强引用吗?
2. 变量local(在方法中定义和使用)是弱引用吗?
3. Phantom reference和Soft reference是怎么出现的?
4. OR以上3个概念无效;意味着仅当您显式使用 java.lang.ref 包中定义的类型时,Java 才会定义引用?

任何帮助对我来说都是很好的。

最佳答案

  1. are listCommon (static variable) and mapInstance (instance variable) Strong reference towards Garbage Collector?

是的,它们是强引用。

  1. Is variable local (defined and used in method) a Weak reference?

不是,是局部变量,所以是变量,所以还是强引用。

  1. How the Phantom reference and Soft reference came in picture?

如果你使用它们。如果您不使用它们,则无需担心它们。它们实际上是为了编写各种缓存。

  1. OR above 3 concepts are invalid; means that Java defines the references only if you explicitly used the type defined in java.lang.ref package?

引用变量总是很强的。其他类型只有在您明确使用它们时才会出现。

关于java - Java 是否隐式定义了对类中使用的对象的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7884810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com