Java: arr==null vs arr.length==0
当 arr 是一个array时,写Java开始的corner case常常会写类似下面的语句:
if(arr == null || arr.length == 0){ return 0; }
其实这是两个条件, arr==null 和 arr.length==0 是不同的:
arr.length == 0说明 arr is an empty array, which means an empty array is assigned to arr; arr.length would yields 0.
arr == null means that null is assigned to arr, arr.length will throw out NullPointerException.
所以在写这两个条件时必须保证arr==null在前。
版权声明:本文为博主原创文章,未经博主允许不得转载。