什么是空?
null是任何东西的实例吗?
null属于哪个集合?
它在内存中是如何表示的?
什么是空?
null是任何东西的实例吗?
null属于哪个集合?
它在内存中是如何表示的?
当前回答
An interesting way to see null in java in my opinion is to see it as something that DOES NOT denote an absence of information but simply as a literal value that can be assigned to a reference of any type. If you think about it if it denoted absence of information then for a1==a2 to be true doesn't make sense (in case they were both assigned a value of null) as they could really could be pointing to ANY object (we simply don't know what objects they should be pointing to)... By the way null == null returns true in java. If java e.g. would be like SQL:1999 then null==null would return unknown (a boolean value in SQL:1999 can take three values : true,false and unknown but in practise unknown is implemented as null in real systems)... http://en.wikipedia.org/wiki/SQL
其他回答
Null是一个特殊值,它不是任何类的实例。下面的程序说明了这一点:
public class X {
void f(Object o)
{
System.out.println(o instanceof String); // Output is "false"
}
public static void main(String[] args) {
new X().f(null);
}
}
Null是一个特殊值,它不是任何东西的实例。很明显,它不能是任何东西的实例。
null关键字是一个表示空引用的文字,它不引用任何对象。Null是引用类型变量的默认值。
也可以看看
null: Java术语
什么是空?
这没什么。
null是任何东西的实例吗?
不,因为它什么也不是,它不可能是任何事物的实例。
null属于哪个集合?
没有任何集合
它在内存中是如何表示的?
如果一些参考指向它,比如:
Object o=new Object();
在堆内存中,分配给新创建对象的空间。o指向内存中指定的空间。
现在 o=null;
这意味着现在o不再指向对象的内存空间。
Java中的Null (tm)
在C和c++中,"NULL"是头文件中定义的常量,其值如下:
0
or:
0L
or:
((void*)0)
取决于编译器和内存模型选项。严格地说,NULL不是C/ c++本身的一部分。
在Java(tm)中,“null”不是关键字,而是null类型的特殊文字。它可以强制转换为任何引用类型,但不能转换为任何基本类型,如int或boolean。null字面量的值不一定是0。并且不可能强制转换为null类型或声明该类型的变量。