自动拆装箱的空指针异常

Integer和基础类型比较时候会自动拆箱, null转为基础类型会报空指针异常。compare(123, null)

public static boolean compare(int a, Integer b) {

//加判空校验 int i = b == null ? 0 : b;

return a == b;//return a == i;}

记录一下,防止以后踩坑。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注