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
Java - is `null` an instance of `object`?
提问by JDelage
I read that null
isn't an instanceof
anything, but on the other hand that everythingin Java extends Object
class.
我读到这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, null
is 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 只是一个可以是任何引用类型的特殊文字
回答by Donal Fellows
回答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.
但是,我们可以将“空”转换为对象。