越界异常
如下图的盒子,只有4个格子,如果从第5个格子里拿东西,肯定是拿不到的。数组也是同理
例子:
public static void main(String[] args) {
int[] arr = {1,2,3};
System.out.println(arr[3]);
}
数组有3个元素,数组的索引就是0,1,2,没有 索引3,因此程序运行后,将会抛出 ArrayIndexOutOfBoundsException 数组越界异常。
空指针异常
观察下面代码:
int[] arr;
System.out.println(arr[0]);
只是声明了数组,没有创建数组,因此运行的时候 会抛出 NullPointerException 空指针异常。