Java null 是一个对象吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1894149/
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-08-12 23:37:57  来源:igfitidea点击:

Is null an Object?

javanull

提问by Registered User

Is null an Objectin Java?

Object在 Java 中是 null吗?

采纳答案by Michael Borgwardt

If null were an Object, it would support the methods of java.lang.Objectsuch as equals(). However, this is not the case - any method invocation on a null results in a NullPointerException.

如果 null 是一个对象,它将支持java.lang.Object诸如equals(). 但是,情况并非如此 - 对 null 的任何方法调用都会导致NullPointerException.

And this is what the Java Language Specificationhas to say on this topic:

这就是Java 语言规范对这个主题的看法:

There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null typeand just pretend that null is merely a special literal that can be of any reference type.

还有一种特殊的 null 类型,表达式 null 的类型,它没有名字。因为 null 类型没有名字,所以无法声明 null 类型的变量或强制转换为 null 类型。空引用是空类型表达式的唯一可能值。空引用始终可以转换为任何引用类型。在实践中,程序员可以忽略 null 类型并假装 null 只是一个可以是任何引用类型的特殊文字。

I think this can be boiled down to "null is special".

我认为这可以归结为“null 是特殊的”。

回答by Erich Kitzmueller

No, it is not an object.

不,它不是一个对象。

回答by cherouvim

No, it's not an instance of a Class nor a Class. It's a reference to nothing.

不,它既不是 Class 也不是 Class 的实例。它是对任何事物的引用。

Edit: haven't read the spec so the above may not be 100% accurate.

编辑:尚未阅读规范,因此上述内容可能不是 100% 准确。

回答by Tommy Carlier

Null is the lack of an object.

Null 是缺少对象。

回答by Darin Dimitrov

Absolutely not: null instanceof Objectreturns false.

绝对不是:null instanceof Object返回false。

回答by Dana the Sane

According to the Java spec, nullis 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 literal nullprovided by the compiler.

根据 Java规范null是一种可以分配给对象变量的类型(作为注释中指出的值)。但是您不能实例化或创建这种类型的变量,您必须使用null编译器提供的文字。

回答by userj29874319284

No, is not an object as null instanceof Object will always return false also there is only one null, not one for each class.

不,它不是一个对象,因为 null instanceof Object 将始终返回 false 并且只有一个 null,而不是每个类都有一个。

回答by Pascal Thivent

As explained in the chapter 4.1 The Kinds of Types and Valuesof the Java Language Specification, null is a type which has one value, the null reference(and is represented by the literal null):

正如Java 语言规范的第4.1类型和值种类中所解释的,null 是一种具有一个值的类型,即 null 引用(由字面量 表示null):

There is also a special nulltype, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that nullis merely a special literal that can be of any reference type.

还有一种特殊的类型,表达式的类型null,它没有名字。因为 null 类型没有名字,所以无法声明 null 类型的变量或强制转换为 null 类型。空引用是空类型表达式的唯一可能值。空引用始终可以转换为任何引用类型。在实践中,程序员可以忽略 null 类型并假装它null只是一个可以是任何引用类型的特殊文字。

You might want to read about the Null Object Pattern(that I don't recommend) though. See the C2 Wikior Wikipediafor more on this pattern.

不过,您可能想了解空对象模式(我不推荐)。有关此模式的更多信息,请参阅C2 WikiWikipedia

回答by Peter Lawrey

According to the Java Spec,

根据Java 规范

There's also a special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types. There's little you can do with a null value beyond testing for its presence. Therefore, null is often used in programs as a marker to indicate that some object is unavailable.

还有一个特殊的空文字可以用作任何引用类型的值。null 可以分配给任何变量,除了基本类型的变量。除了测试它的存在之外,您几乎无法对空值做任何事情。因此,程序中经常使用 null 作为标记,以指示某些对象不可用。

回答by Thorbj?rn Ravn Andersen

No. Even if it was it was, it is useless as it does not have any methods or fields.

不。即使是它,它也没有任何用处,因为它没有任何方法或字段。