Java:为什么 NullPointerException 不称为 NullReferenceException?

发布于 2024-07-04 17:30:35 字数 26 浏览 3 评论 0原文

这是一个疏忽吗? 还是跟JVM有关?

Was this an oversight? Or is it to do with the JVM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

会傲 2024-07-11 17:30:35

我想这与 JVM 是用 C++ 编码的事实有关。 除此之外,指针和引用几乎相似。 您可以说 Java 中的引用机制是使用 C++ 指针实现的,并且名称“NullPointerException”让实现细节得以凸显。

I guess it has to do with the fact that the JVM is coded in C++. Apart from that, pointers and references are nearly similar. You could say that the reference mechanism in Java is implemented using C++ pointers and the name 'NullPointerException' allows that implementation detail to shine through.

迷你仙 2024-07-11 17:30:35

我认为答案是我们永远不会真正知道真正的答案。

我怀疑在 Java 1.0 发布之前的某个时间,有人定义了一个名为 NullPointerException 的类。 要么是那个人把名字弄错了,要么是术语还没有稳定下来; 即尚未做出使用术语“引用”而不是“指针”的决定。

无论哪种方式,当发现不一致1时,很可能无法在不破坏向后兼容性的情况下修复它。

但这只是猜测。


1 - 如果您认真观察,您可以在标准 Java SE 库中找到其他类似的小不一致之处。 还有一些更严重的问题由于同样的原因而无法解决。

I think that the answer is that we'll never really know the real answer.

I suspect that some time before Java 1.0 was released, someone defined a class called NullPointerException. Either that person got the name wrong, or the terminology hadn't stabilized; i.e. the decision to use the term "reference" instead of "pointer" hadn't been made.

Either way, it is likely that by the time the inconsistency1 was noticed it couldn't be fixed without breaking backwards compatibility.

But this is just conjecture.


1 - You can find other minor inconsistencies like this in the standard Java SE libraries if you look really hard. And a few more serious issues that couldn't be fixed for the same reason.

与君绝 2024-07-11 17:30:35

在 Java 中,他们使用术语 REFERENCE 来引用动态创建的对象。 在以前的语言中,它被称为 POINTER。 正如面向对象语言中方法的命名已经取代了早期(面向对象和非面向对象)语言中的函数和过程的命名一样。 这个命名或新标准没有特别的好坏,它只是对能够创建以前的动态对象、悬挂在空中(堆空间)并由固定对象引用引用的现象的另一种命名方式(或动态参考也悬挂在空中)。 新标准(方法和参考文献的使用)的作者特别提到,实施新标准是为了使其符合即将推出的规划系统 - 例如 UML 和 UP,其中面向对象术语的使用规定了属性的使用,方法和引用,而不是变量、函数和指针。

现在,您可能会认为这只是一个重命名,但这将简化过程的描述,而不仅仅是语言以发展的名义所走的漫长道路。 方法本质上与过程不同,方法在类的范围内运行,而过程本质上在全局范围内运行。 类似地,属性不仅仅是变量的新名称,因为属性是类(及其实例 - 对象)的属性。 类似地,引用(这是这里这篇小文章背后的问题)的不同之处在于它是类型化的,因​​此它不具有指针的典型属性,是对内存单元的原始引用,因此同样,引用是对基于内存的对象。

现在,在 Java 内部,即在 Java 的内部,底层 C 代码和 Java 代码(虚拟机)之间有一个仲裁层。 在该仲裁层中,出现了抽象,因为引用的底层(裸机)实现(如虚拟机所做的那样)保护引用免于引用裸机(没有结构的存储单元)。 因此,原始事实是,NullPointerException 确实是空指针异常,而不仅仅是空引用异常。 然而,这个事实对于 Java 环境中的程序员来说可能完全无关,因为他/她在任何时候都不会接触裸机 JVM。

In Java they use the nomenclature REFERENCE for referring to dynamically created objects. In previous languages, it is named POINTER. Just as the naming of METHODS in object oriented languages has taken over from former FUNCTION's and PROCEDURE's in earlier (object oriented as well as not object oriented) languages. There is no particular better or worse in this naming or new standard, it is just another way of naming the phenomenon of being able to create former dynamic objects, hanging in the air (heap space) and being referred to by a fixed object reference (or dynamic reference also hanging in the air). The authors of the new standard (use of METHODS and REFERENCES) particularly mentioned that the new standards were implemented to make it conformant with the upcoming planning systems - such as UML and UP, where the use of the Object oriented terminology prescribes use of ATTRIBUTES, METHODS and REFERENCES, and not VARIABLES, FUNCTIONS and POINTERS.

Now, you may think now, that it was merely a renaming, but that would be simplifying the description of the process and not be just to the long road which the languages have taken in the name of development. A method is different in nature from a procedure, in that it operates within a scope of a class, while the procedure in its nature operates on the global scope. Similarly an attribute is not just a new name for variable, as the attribute is a property of a class (and its instance - an object). Similarly the reference, which is the question behind this little writing here, is different in that it is typed, thus it does not have the typical property of pointers, being raw references to memory cells, thus again, a reference is a structured reference to memory based objects.

Now, inside of Java, that is - in the stomach of Java, there is an arbitration layer between the underlying C-code and the Java code (the Virtual Machine). In that arbitration layer, an abstraction occurs in that the underlying (bare metal) implementation of the references, as done by the Virtual machine, protects the references from referring bare metal (memory cells without structure). The raw truth is therefore, that the NullPointerException is truly a Null Pointer Exception, and not just a Null Reference Exception. However, that truth may be completely irrelevant for the programmer in the Java environment, as he/she will not at any point be in contact with the bare metal JVM.

饮惑 2024-07-11 17:30:35

Java 确实有指针——不能对其执行指针算术的指针。

来自古老的 JLS

Java 编程语言中有两种类型:基本类型(第 4.2 节)和引用类型(第 4.3 节)。 相应地,有两种数据值可以存储在变量中、作为参数传递、由方法返回并进行操作:原始值(第 4.2 节)和引用值(第 4.3 节)。

稍后

对象类实例数组

引用值(通常只是引用)是指向这些对象的指针,以及一个特殊的空引用,它不引用任何对象。

(强调他们的)

因此,为了解释,如果您编写:

Object myObj = new Object();

那么 myObj 是一个引用类型,它包含一个引用值,它本身就是一个 < em>指向新创建的对象的指针。

因此,如果将 myObj 设置为 null,则将参考值(又名指针)设置为 null。 因此,当变量被取消引用时,会合理地抛出 NullPointerException。

别担心:这个话题已经热烈讨论过< /a> 之前。

Java does indeed have pointers--pointers on which you cannot perform pointer arithmetic.

From the venerable JLS:

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).

And later:

An object is a class instance or an array.

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

(emphasis theirs)

So, to interpret, if you write:

Object myObj = new Object();

then myObj is a reference type which contains a reference value that is itself a pointer to the newly-created Object.

Thus if you set myObj to null you are setting the reference value (aka pointer) to null. Hence a NullPointerException is reasonably thrown when the variable is dereferenced.

Don't worry: this topic has been heartily debated before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文