编译时都不会报错。
1.数组下标越界
java.lang.ArrayIndexOutOfBoundsException
//下标越界
int[] arr = new int[]{1,2,3,4,5};
for (int i = 0; i <= arr.length; i++) { //i可以取到5
System.out.println(arr[i]);
}
System.out.println(arr[-2]);
2.空指针异常
java.lang.NullPointerException
//空指针异常
int[] arr1 = new int[]{1,2,3};
arr1 = null;
System.out.println(arr1[0]);
int[][] arr2 = new int[4][]; //二维数组中的一维数组为null
System.out.println(arr2[0][0]);
String[] arr3 = new String[]{"AA","BB","CC"};
arr3[0] = null;
System.out.println(arr3[0].toString());
标签:Java,int,越界,数组,new,下标,异常,指针
From: https://www.cnblogs.com/zhishu/p/16617534.html