try {
System.out.println( arr[(Integer) null]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这种方式使用数组会跑Nullpointer异常那个,因为数组中无null位置的元素。并且查看具体异常的跟踪信息(stackTrace),显示如下:
org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.
同理,无法使用这种方式 byte b = (Byte) null ;(同理Integer、Long等整数类型封装类)
无法将null转型为byte;
因为
byte内部又做缓存,同样也会导致上面数组那样的空指针。
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}