码迷,mamicode.com
首页 > 编程语言 > 详细

数组中array==null和array.length==0的区别

时间:2019-03-15 01:09:19      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:file   ati   不能   rgs   str   new   ini   ESS   异常   

//代码
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是不能调用方法的,所以会报空指针异常

 

数组中array==null和array.length==0的区别

标签:file   ati   不能   rgs   str   new   ini   ESS   异常   

原文地址:https://www.cnblogs.com/JohnTeslaaa/p/10534471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!