Java:为什么NullPointerExceptions不是NullReferenceExceptions? [英] Java: Why aren't NullPointerExceptions called NullReferenceExceptions?

查看:94
本文介绍了Java:为什么NullPointerExceptions不是NullReferenceExceptions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是疏忽吗?或者它与JVM有关吗?

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

推荐答案

Java确实有指针 - 你无法执行指针运算的指针。

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

来自古老的 JLS


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

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

以后


对象类实例数组

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

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

(强调他们的)

所以,如果你写的话要解释:

So, to interpret, if you write:

Object myObj = new Object();

然后 myObj 参考类型,其中包含引用值,它本身是新创建的对象指针

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

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

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.

这篇关于Java:为什么NullPointerExceptions不是NullReferenceExceptions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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