什么是空?

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是任何东西的实例吗?

不,因为它什么也不是,它不可能是任何事物的实例。

null属于哪个集合?

没有任何集合

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

如果一些参考指向它,比如:

Object o=new Object();

在堆内存中,分配给新创建对象的空间。o指向内存中指定的空间。

现在 o=null;

这意味着现在o不再指向对象的内存空间。

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

也可以看看

null: Java术语

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

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

Null不是任何类的实例。

然而,你可以将null赋值给任何类型的变量(对象或数组):

 // this is false   
 boolean nope = (null instanceof String);

 // but you can still use it as a String
 String x = null;
 "abc".startsWith(null);