文章目录
- 1. 场景案例
- 2. 规避自动拆箱引发的空指针的建议
 
 
 
 
 
1. 场景案例
package com.gblfy;/*** 自动拆箱引起的空指针问题场景*/
public class Unboxingnpe {private static int add(int x, int y) {return x + y;}private static boolean compare(long x, long y) {return x >= y;}public static void main(String[] args) {//1.变量赋值自动拆箱出现的空指针Long count =null;long _count =count;//2.方法传参时自动拆箱引发的空指针
//        Integer left = null;
//        Integer right = null;
//        System.out.println(add(left, right));//3.用于大小比较的场景
//        Long left=10L;
//        Long right=null;
//        System.out.println(compare(left,right));}
}2. 规避自动拆箱引发的空指针的建议
1.基本数据类型由于包装器类型,优先考虑使用基本数据类型
 2.对于不确定的包装器类型,一定要校验是否是NULL
 3.对于值为NULL的包装器类型,赋值为0