是否有“解除引用”的正式或权威定义?在Java? [英] Is there a formal or authoritative definition for "dereferencing" in Java?

查看:103
本文介绍了是否有“解除引用”的正式或权威定义?在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如,ref是一个引用,当用于访问它引用的Integer对象时,它被解除引用:

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

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

当解除引用流程失败时会发生NullPointerExceptions:

And NullPointerExceptions occur when that dereferencing process fails:

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

但是,我的解释与在Oracle的新 Java SE 8程序员I考试(测试版)

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



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

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


所以根据创建Java 8考试的人来说,取消引用Java是重新分配参考的行为,而不是评估参考的行为:

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:

例如:

    // 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.

谷歌搜索该问题表明甲骨文的定义被更广泛地使用,但这并不意味着它是对于新的Java 8考试而言,谷歌中唯一一个通过重新分配取消引用的命中率!无论如何,JLS并没有真正解决问题。

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.

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

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?)

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

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

推荐答案

Java变量是 Oracle的术语(除了特殊类型的null之外)。我从C ++背景来到Java,并且总是发现最容易想到像指针这样的引用类型,因为它最容易理解它们是如何工作的(尽管有重要差异),但是因为所有非原始变量都是这样的,所以没有评估的概念(没有相当于C ++的取消引用指针)。

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).

所以,在你的例子中:


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


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

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!).

AFAIK在Java中没有解除引用的官方定义。因为评估和分配之间没有区别,所以它确实有意义,尽管它不是我认为广泛使用的术语。

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.

这篇关于是否有“解除引用”的正式或权威定义?在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆