weihua0624

越界异常和空指针异常

/*
  数组操作的两个常见小问题:
  1.ArrayIndexOutOfBoundsException:数组索引越界异常
  原因:访问了不存在的索引。

  2.NullPointerException:空指针异常
  原因:数组已经不在指向堆内存了。而你还用数组名去访问元素。
*/
class Exception {
  public static void main(String[] args) {
    //定义数组
    int[] arr = {1,2,3};
    //System.out.println(arr[3]);

    //引用类型的常量:空常量 null
    arr = null;
    System.out.println(arr[0]);
  }
}

分类:

技术点:

相关文章:

  • 2021-11-29
  • 2021-12-03
  • 2019-12-26
  • 2021-10-20
  • 2021-12-28
  • 2021-05-31
  • 2021-11-24
  • 2021-06-24
猜你喜欢
  • 2021-11-29
  • 2021-08-31
  • 2021-11-27
  • 2018-06-16
  • 2021-12-05
  • 2021-11-29
相关资源
相似解决方案