null 是一个对象吗?
null 是 Java 中的对象
吗?
Is null an Object
in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
null 是 Java 中的对象
吗?
Is null an Object
in Java?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(14)
如果 null 是一个对象,它将支持
java.lang.Object
的方法,例如equals()
。然而,情况并非如此 - 对 null 的任何方法调用都会导致NullPointerException
。这就是 Java 语言规范< /a> 关于这个话题不得不说:
我认为这可以归结为“null 是特殊的”。
If null were an Object, it would support the methods of
java.lang.Object
such asequals()
. However, this is not the case - any method invocation on a null results in aNullPointerException
.And this is what the Java Language Specification has to say on this topic:
I think this can be boiled down to "null is special".
根据 Java 规范,
null
是一种可以分配给对象变量的类型(作为注释中注明的值)。但您无法实例化或创建这种类型的变量,您必须使用编译器提供的文字null
。According to the Java spec,
null
is a type that can be assigned to an object variable (as a value as noted in the comment). You cannot instantiate or create variables of this type though, you must use the literalnull
provided by the compiler.绝对不是:
null instanceof Object
返回 false。Absolutely not:
null instanceof Object
returns false.JRL(和其他人)写道:
通常,这取决于您从哪里看待它,您更相信谁。
根据 JLS 的说法,是的。特别是如果您将问题改写为:“
null
文字是Object
类型吗?”。除了 JLS 4.1 上面由 Michael Borgwardt 引用:
参见 JLS 3.10.7:
和 JLS 4.10:
或 JLS 4.10.2:
[我强调。]
根据 Eclipse 2019-09 的编译器不是:
根据 OpenJDKs 12.0.1
javac
它是:角度在哪里括号暗示
null
不是原始类型。并根据 JLS 4.1:如果不是这个,那就是另一个。
克劳迪乌写道:
相反,
null
很漂亮。您建议将什么作为引用类型变量的默认值?任意位组合?欢迎来到访问冲突,或者更糟糕的是,指针地狱!约阿希姆·索尔写道:
实际上有三个项与null相关(另请参见JLS 3.10.7):(
null
文字。(1) 请注意,根据JLS 4.10.2 上面引用的,null 类型使用多个继承不仅适用于接口,也适用于类。众所周知,这对于我们应用程序员来说是不可能的。
(2) null 文字 可以想象为一个变量,定义如下:
JVM_global Final null_type null = new null_type();
另请注意 JLS 3.9:
null instanceof
关于 JLS 4.10.2 的 请记住(“null 类型 是每种类型的子类型”)
null instanceof
应该计算为true
,不应该吗?乍一看,是的,但是 JLS 15.20.2 给出了深刻的答案:[我强调。]
问问自己什么更有意义(从应用程序程序员的角度来看):
给出
false
,从而表明引用表达式不是暴露给我们的类型,即表明它没有引用任何对我们有用的东西或给出
true
,从而告知我们知道表达式的计算结果是一个特殊的引用,空引用,引用一个“对象”,我们不知道它是否存在以及它是特殊的空类型它没有名字,不会暴露给我们,但通过空文字,是任何类型的子类型,包括多重继承,并且无论如何都会被忽略?还请考虑更实际的示例:这也导致:
为什么
instanceof
不是描述null
的对象性的正确方法?它被称为
instanceof
,而不是sameorsubtypeof
。这意味着我们正在将实例的类型与一个类型进行比较,而不是两个类型。现在null
的意思是:“没有实例”,如果没有实例,则没有实例的类型。很明显,将任何东西与某物进行比较应该会导致错误
。或者在“更”现实世界的示例中:
正如迈克尔总结的那样:“null 确实很特别”。
JRL (and others) wrote:
As often, it depends from where you look at it, who you believe more.
According to the JLS, yes, it is. Especially if you rephrase the question to: „Is the
null
literal of typeObject
?”.In addition to JLS 4.1 cited by Michael Borgwardt above:
See JLS 3.10.7:
and JLS 4.10:
or JLS 4.10.2:
[Emphases by me.]
According to Eclipse 2019-09's compiler it's not:
According to OpenJDKs 12.0.1
javac
it is:Where the angle brackets imply that
null
is of an other than a primitive type. And according to JLS 4.1:if it's not the one it's the other.
Claudiu wrote:
Au contraire,
null
is beautiful. What would you suggest as default value for a reference type variable instead? An arbitrary bit combination? Welcome to access violation or, even worse, pointer hell!Joachim Sauer wrote:
There are actually three items in conjunction with null (see also JLS 3.10.7):
null
literal.(1) Note that, according to JLS 4.10.2 cited above, the null type uses multiple inheritance not only for interfaces but for classes as well. Which we all know is not possible for us application programmers.
(2) The null literal might be imagined as a variable being defined as:
JVM_global final null_type null = new null_type();
Note also JLS 3.9:
Concerning
null instanceof <any type>
With JLS 4.10.2 in mind („the null type is a subtype of every type”)
null instanceof <any type>
should be supposed to evaluate totrue
, shouldn't it? At first sight, yes, but JLS 15.20.2 gives the insight answer:[Emphases by me.]
Ask yourself what makes more sense (from an application programmer's point of view):
Giving
false
and thus indicating that a reference expression is not of a type exposed to us, i.e. indicating it's not referencing anything useful to usor giving
true
, thus informing us that the expression evaluates to a special reference, the null reference, referencing an "object" we don't know whether it even exists and which is of the special null type which has no name, is not exposed to us but via the null literal, is a subtype of any type including multiple inheritance and is to be ignored anyway? Consider also the more practical example:Which also leads to:
Why is
instanceof
not a proper way to say something aboutnull
's Object-ness?It's called
instanceof
notsameorsubtypeof
. That means we are comparing an instance's type with a type, not two types. Nownull
means: „There is no instance” and if there is no instance there's no instance's type. It's obvious that comparing nothing with something is supposed to lead tofalse
.Or in a "more" real world example:
As Michael sums up: "null is special" indeed.
Null 是指缺少对象。
Null is the lack of an object.
不,它不是一个物体。
No, it is not an object.
不,它不是类的实例,也不是类的实例。这是对任何事物的引用。
编辑:尚未阅读规范,因此上述内容可能不是 100% 准确。
No, it's not an instance of a Class nor a Class. It's a reference to nothing.
Edit: haven't read the spec so the above may not be 100% accurate.
正如4.1 类型和值的种类<一章中所述根据 Java 语言规范,null 是一种只有一个值的类型,即 null 引用(由文字
null
表示):不过,您可能想阅读空对象模式(我不推荐)。请参阅 C2 Wiki 或 Wikipedia 了解有关此模式的更多信息。
As explained in the chapter 4.1 The Kinds of Types and Values of the Java Language Specification, null is a type which has one value, the null reference (and is represented by the literal
null
):You might want to read about the Null Object Pattern (that I don't recommend) though. See the C2 Wiki or Wikipedia for more on this pattern.
不,不是一个对象,因为 null instanceof Object 总是返回 false,而且只有一个 null,而不是每个类都有一个。
No, is not an object as null instanceof Object will always return false also there is only one null, not one for each class.
根据 Java 规范,
According to the Java Spec,
Java 通过引用来处理对象。 Null 是 Java 面向对象的崩溃,因为它使你低于面向对象的水平。不,它不是一个对象,而是一个引用的值。它与对象范例无关,但与支持对象的 Java 管道有关。
Java handles objects via references. Null is a breakdown of OO-ness of Java, since it drops you below OO level. No it is not an object it is a VALUE of a reference. And it has nothing to do with object paradigms, but relates to plumbing of Java, that enables objects.
null 是 java.lang.Object 的实例吗?不。null
是一个对象吗?取决于“is”的定义。
Is null an instance of
java.lang.Object
? No.Is null an object? depends on the definition of "is".
第一行显示
null
可以分配给类型Object
,但第二行将证明它肯定不是Object
并最终导致java.lang.NullPointerException
The first line shows
null
can be assigned to typeObject
, but the second line will demonstrate it is certainly not anObject
and eventually results in ajava.lang.NullPointerException
不,
null
不是一个对象。它是一个引用类型,其值不引用任何对象,因此内存中没有null
的表示。No ,
null
is not an object.It is a reference type and its value does not refer to any object and so there is no representation ofnull
in memory.