我在 FirstRain 被问到这个问题
当一个人做这样的事情时,
int j = null;
它导致类型不匹配:无法将 null 转换为 int。
现在
Integer i = null;
int n = i;
System.out.println(n);
为什么打印 n 会导致 NullPointerException,即使原始数据类型不允许为 null。
为什么它没有导致类型不匹配:无法将 null 转换为 int。
最佳答案
实际上是 int n = i;
在 Integer
拆箱时抛出 NullPointerException
,因为拆箱涉及取消引用存储的空引用在 i
中。
关于java - 自动装箱后将 null 分配给原始数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31023780/