什么是空?

null是任何东西的实例吗?

null属于哪个集合?

它在内存中是如何表示的?


当前回答

null关键字是一个表示空引用的文字,它不引用任何对象。Null是引用类型变量的默认值。

也可以看看

null: Java术语

其他回答

Java中的Null (tm)

在C和c++中,"NULL"是头文件中定义的常量,其值如下:

    0

or:

    0L

or:

    ((void*)0)

取决于编译器和内存模型选项。严格地说,NULL不是C/ c++本身的一部分。

在Java(tm)中,“null”不是关键字,而是null类型的特殊文字。它可以强制转换为任何引用类型,但不能转换为任何基本类型,如int或boolean。null字面量的值不一定是0。并且不可能强制转换为null类型或声明该类型的变量。

不,它不是任何东西的实例,instanceof总是false。

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

简短准确的回答,正式回答您的所有问题:

3.10.7. 空文字 空类型有一个值,即空引用,由 null literal null,由ASCII字符组成。 空字面值总是null类型。

只有赋值为null类型的引用才会被分配。您不需要为引用分配任何值(对象)。 这样的分配取决于JVM将占用多少引用以及将在哪个内存区域分配引用。

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);
   }
}