题
public class Test {
Integer i;
int j;
public static void main ( String [] args ) {
Test t = new Test ();
t.go();
}
public void go() {
j=i;
System.out.println(j);
System.out.println(i);
}
}
输出 : 线程“ main” java.lang.nullpointerexception in test.go(test.java:12)在test.main(test.java:8)中的异常
解决方案
这显然不是错误。您将获得一个运行时nullpoInterException,因为您正在拆箱NULL引用(i
)进入原始(j
)。见JLS §5.1.8.
原因 i
null是该实例字段初始化为0,null或false。
不隶属于 StackOverflow