Integer a = null; int b=a;

2018-11-29 22:39:31

如题执行下面的方法会发生什么?

Integer a = null;
int b=a;

这个话题牵扯到java的基本数据类型对应的对象类型的自动拆箱,首先我们考虑下下面的代码的执行:

 Integer a = 1;
 int b=a;

我们把断点打在Integer.intValue()方法上,发现int b= a 就是执行 Integer(1).intValue() 所以

Integer a = null;
int b = a;

等于
int b = null.intValue();
会抛出空指针异常

Gitalking ...

Markdown is supported

Be the first guy leaving a comment!