Java - `null` 是 `object` 的实例吗?

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

Java - is `null` an instance of `object`?

java

提问by JDelage

I read that nullisn't an instanceofanything, but on the other hand that everythingin Java extends Objectclass.

我读到这null不是instanceof什么,但另一方面,Java中的所有内容都扩展了Object类。

采纳答案by JDelage

No, it is a reference. null is not an object

不,它是一个reference. null 不是对象

String s = null;

System.out.println(s instanceof Object); // false

回答by stacker

Null means that you don't have a reference to an object.

Null 意味着您没有对对象的引用。

Object o = null;

o is a reference but there is no object referenced and no memory allocated for it.

o 是一个引用,但没有引用对象,也没有为其分配内存。

o = new Object();

o is still a reference and holds the adress where the object is located in memory

o 仍然是一个引用并保存对象在内存中的地址

回答by mikej

No, nullis not an object. It is a literal that means that a variable does not reference any object.

不,null不是对象。它是一个文字,表示变量不引用任何对象。

回答by NPE

In a word, no.

一句话,没有。

Peter Norvig's Java IAQaddresses this question in some detail (specifically "Q: Is null an Object?")

Peter Norvig 的Java IAQ详细解决了这个问题(特别是“ Q: Is null an Object?”)

回答by saugata

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 type and just pretend that null is merely a special literal that can be of any reference type

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

Java Language Specification

Java 语言规范

回答by Donal Fellows

As the JLSsays, nullis of the null type and that is not a reference type. It is however usable in situations where a value of a reference type is expected (the value is really a “bottom” in the type algebra).

正如JLS所说,null是空类型,而不是引用类型。然而,它可用于需要引用类型值的情况(该值实际上是类型代数中的“底部”)。

回答by Mitali Jain

Hoping that this example clears your doubt :

希望这个例子能消除你的疑虑:

public void printObject(Object object) {
    System.out.println("object referred");
}

public void printObject(double[] doubleArray) {
    System.out.println("Array of type double");
}

public static void main(String[] args) {
    JavaNullObjectTest javaNullObjectTest = new JavaNullObjectTest();

    javaNullObjectTest.printObject(null); //Array of type double

    javaNullObjectTest.printObject((Object)null); //object referred
}

We can but, convert a 'null' to object.

但是,我们可以将“空”转换为对象。