Integer 变量为 null 时自动拆箱会怎样?
参考答案
将 null 的 Integer 自动拆箱为 int 时会抛出 NullPointerException,因为拆箱需要调用该对象的方法取得基本类型值。
题目解析
比较包装类型时也要区分引用、数值和 null,避免依赖 Integer 缓存范围判断相等。
将 null 的 Integer 自动拆箱为 int 时会抛出 NullPointerException,因为拆箱需要调用该对象的方法取得基本类型值。
比较包装类型时也要区分引用、数值和 null,避免依赖 Integer 缓存范围判断相等。