//代码
public
class Test1 { public static void main(String[] args) { int[] a1 = new int[0]; int[] a2 = null; System.out.println(a1.length);//0 System.out.println(a2.length);//NullPointerException
} }

 

//输出结果

"C:\Program Files\Java\jdk1.8.0_191\bin\java" 


0
Exception in thread "main" java.lang.NullPointerException
at LinkedList.Test1.main(Test1.java:8)

Process finished with exit code 1

解析:

a1 表示给数组分配了地址,但是还没有存东西;

a2表示连地址都没有分配,就是个空的

 

而null是不能调用方法的,所以会报空指针异常

 

相关文章:

  • 2022-12-23
  • 2022-02-12
  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-04-27
  • 2022-12-23
  • 2021-09-18
  • 2021-06-25
  • 2021-12-03
  • 2021-11-18
相关资源
相似解决方案