Java中 null是一个对象还是一个值?
------解决方案--------------------
好吧,我是有病!工作 11 年了,第一次碰到你这样的人,不知好歹!
这里是严谨的技术论坛,我有必要在这乱说一气么?
Java 中有 53 个保留字,其中 50 个是关键字,还有 3 个是值!
Java Language Specification
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.9
3.9. Keywords
50 character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (§3.8).
Keyword: one of
abstract continue for new switch
assert default if package synchronized
boolean do goto private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
The keywords const and goto are reserved, even though they are not currently used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs.
While true and false might appear to be keywords, they are technically Boolean literals (§3.10.3). Similarly, while null might appear to be a keyword, it is technically the null literal (§3.10.7).
------解决方案--------------------
java由C演化而来,所以很多东西跟C很相似
在C里,null是个值,它的值是0,true,false也是值,false是0,true是0以外的值,一般是1或-1
所以java里应该也继续沿用null这个值
可以通过javap来验证一下
public class jp {
public static void main(String[] args) {
Object o = null;
boolean b1 = false;
boolean b2 = true;
}
}
java jp //编译
javap -c jp //反编译查看伪代码
E:\Test>javap -c jp
Compiled from "jp.java"
public class jp {
public jp();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":
()V
4: return
public static void main(java.lang.String[]);
Code:
0: aconst_null
1: astore_1
2: iconst_0
3: istore_2
4: iconst_1
5: istore_3
6: return
}
可见null是个常量 aconst_null(即null指针), false也是个常量 iconst_0(即int的0), true是常量 iconst_1(即int的i)