11.1 ArrayIndexOfBoundsException
数组索引越界异常:就是访问了数组中不存在的索引时发生的异常。
示例:
/**
* ArrayIndexOutOfBoundsException:数组索引越界异常
*
* @author maxiaoke.com
* @version 1.0
*/
public class ArrayTest10 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println(arr[3]); // Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
}
}
11.2 NullPointerException
空指针异常:数组没有指向堆内存的空间,我们却试图去访问堆内存空间中的值。
示例:
/**
* NullPointerException:空指针异常
*
* @author maxiaoke.com
* @version 1.0
*/
public class ArrayTest11 {
public static void main(String[] args) {
int[] arr = null;
System.out.println(arr[0]); // Exception in thread "main" java.lang.NullPointerException
}
}