1、ArrayIndexOutOfBoundsException:数组索引越界异常
原因:访问了不存在的索引时,会出现下标(索引越界异常)。
int[] arr = new int[5];
System.out.println(arr[6]);
结果:报下标越界异常。
2、NullPointerException:空指针异常
原因:数组已经不在指向堆内存了,而你还用数组名去访问元素。
int[] arr = new int[];
arr = null; //将数组引用为null
System.out.println(arr[0]);
结果:报空指针异常。
这里说一下,如果堆内存中的数据,如果没有引用指向它,过阶段就会被垃圾回收机制回收,被JVM虚拟机释放掉。