gpt4 book ai didi

java - Oracle Java 教程 - 回答问题时可能出错

转载 作者:太空狗 更新时间:2023-10-29 22:33:46 24 4
gpt4 key购买 nike

我是 Java 新手,正在阅读 Oracle 教程。每节后都有问题和答案,我在一个答案中听不懂一个句子(见下面的粗线)。

来源是https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html

我指的是问题 2,请参见粗体字。据我了解,如果没有对数组的引用,则数组有资格进行垃圾回收。是否存在对该数组所持有对象的引用并不重要,因为内部对象(在数组内)有自己的引用计数。那正确吗?请解释加粗的句子。

引用自 oracle 教程:https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html

  1. Question: The following code creates one array and one string object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection?

    String[] students = new String[10];
    String studentName = "Peter Smith";
    students[0] = studentName;
    studentName = null;

    Answer: There is one reference to the students array and that array has one reference to the string Peter Smith. Neither object is eligible for garbage collection. The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null. The object studentName is not eligible either because students[0] still refers to it.

最佳答案

The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null.

是的,那句话很……奇怪。这没有意义。

无论数组对其他对象的引用如何,它都可以进行垃圾回收。

students 是对数组的引用,因此只要 students 仍在范围内,它就没有资格进行垃圾回收。

关于java - Oracle Java 教程 - 回答问题时可能出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52095760/

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