注意当一个引用类型为空的包装类型的数据传递给一个基础数据类型的,在转换过程中会出现空指针异常;建议在转换前判或用一致类型的,这个问题比较基础但容易犯错
Integer max = null; search(max); System.out.println("ok");
源码中有一段话,说明了会直接抛空指针异常
public static int parseInt(String s, int radix)
throws NumberFormatException
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
if (s == null) {
throw new NumberFormatException("null");
}
.........
}
public static void search(int num){ return; }
Exception in thread "main" java.lang.NullPointerException
at com.unique.User.main(User.java:34)