Java 中的“取消引用”是否有正式或权威的定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26153618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:23:12  来源:igfitidea点击:

Is there a formal or authoritative definition for "dereferencing" in Java?

java

提问by skomisa

Coming from a C and C++ background, I have always assumed that dereferencing in Java is the process of accessing an object via its reference.

来自 C 和 C++ 背景,我一直认为在 Java 中取消引用是通过引用访问对象的过程。

For example, "ref" is a reference, and it is dereferenced when used to access the Integer object it refers to:

例如,"ref" 是一个引用,当用于访问它所引用的 Integer 对象时,它会被取消引用:

    Integer ref = new Integer(7);     
    // Dereference "ref" to access the object it refers to.
    System.out.println(ref.toString()); 

And NullPointerExceptions occur when that dereferencing process fails:

当取消引用过程失败时,会发生 NullPointerExceptions:

Integer ref = null;
// Dereferencing "ref" causes an exception.
System.out.println(ref.toString()); 

However, my interpretation conflicts with one of the topics being tested on Oracle's new Java SE 8 Programmer I exam (beta):

但是,我的解释与 Oracle 新的Java SE 8 Programmer I 考试(测试版)中正在测试的主题之一相冲突:

Explain an Object's Lifecycle (creation, "dereference by reassignment" and garbage collection)

解释对象的生命周期(创建、“通过重新分配取消引用”和垃圾收集)

So according to whoever created the Java 8 exam, dereferencing in Java is the act of reassigning a reference, rather than the act of evaluating a reference:

因此,根据创建 Java 8 考试的人的说法,在 Java 中取消引用是重新分配引用的行为,而不是评估引用的行为:

For example:

例如:

    // Create an Integer object, and a reference to it.
    Integer ref = new Integer(7); 
    ref = null; 
    // Now (according to Oracle?):
    // - The reassignment means ref has been "dereferenced".
    // - The dereferenced object is now eligible for Garbage Collection.

Googling the issue anecdotally suggests that Oracle's definition is more widely used, but that doesn't mean it is correct, and the only hit on google for "dereference by reassignment" is for that new Java 8 exam! The JLS doesn't really shed any light either way.

谷歌搜索这个问题的轶事表明 Oracle 的定义被更广泛地使用,但这并不意味着它是正确的,谷歌上唯一的“通过重新分配取消引用”是针对新的 Java 8 考试!无论哪种方式,JLS 都没有真正发挥作用。

Is there any formal or authoritative definition (as opposed to personal opinions) on what dereferencing really means in Java? (i.e. Does it relate to evaluation or reassignment?)

关于取消引用在 Java 中的真正含义,是否有任何正式或权威的定义(而不是个人意见)?(即它与评估或重新分配有关吗?)

It seems strange that two completely different definitions manage to coexist.

两个完全不同的定义设法共存似乎很奇怪。

回答by Yogesh Umesh Vaity

Although there is no official definition, the word 'dereference' is used in some of the Java error statements.

尽管没有官方定义,但在某些 Java 错误语句中使用了“取消引用”一词。

For example, if you write following code:

例如,如果您编写以下代码:

char ch = 'A';
if (ch.isLetter()) { }

You get error: char cannot be dereferenced

你得到错误: char cannot be dereferenced

So, one can say that accessing the state or behaviour of an object using its reference with the help of the .operator is dereferencing.

因此,可以说在.运算符的帮助下使用对象的引用访问对象的状态或行为是取消引用。

回答by user3757014

Java variables are either "primitive types" or "reference types" in Oracle's terminology(well, other than the special type of null). I came to Java from a C++ background and always found it easiest to think of reference types like pointers as that makes it easiest to understand how they work (though there are important differences), however because all non-primitive variables are like that there's no concept of evaluation (no equivalent of C++'s dereferencing of pointers).

Java 变量在Oracle 的术语中要么是“原始类型”,要么是“引用类型” (好吧,除了特殊类型的 null)。我是从 C++ 背景开始接触 Java 的,总是发现最容易想到像指针这样的引用类型,因为这样最容易理解它们的工作原理(尽管存在重要差异),但是因为所有非原始变量都是这样的,没有评估的概念(不等同于 C++ 对指针的取消引用)。

So, in your example:

所以,在你的例子中:

Integer ref = new Integer(7); 
ref = null;
Integer ref = new Integer(7); 
ref = null;

On the first line an object is created on the heap and ref refers to it. On the second line ref is changed to refer to null. There are no more references to the object on the heap so it will have become eligible for garbage collection, but until such time as the garbage collector does so it will actually still be there (excluding any clever JVM optimisation of this simple example!).

第一行在堆上创建一个对象,ref 引用它。在第二行 ref 更改为引用 null。堆上不再有对对象的引用,因此它将有资格进行垃圾收集,但是直到垃圾收集器这样做之前,它实际上仍然存在(不包括这个简单示例的任何巧妙的 JVM 优化!)。

AFAIK there isn't an official definition of "dereferencing" in Java. Because there isn't the distinction between evaluation and assignment it does make sense, though it's not a term that I think is widely used.

AFAIK 在 Java 中没有“取消引用”的官方定义。因为评估和赋值之间没有区别,所以它确实有意义,尽管我认为它不是一个被广泛使用的术语。