有什么原因
int[] myArray = new int[0];
编译?
这样的表达有什么用吗?
myArray[0] = 1;
给java.lang.ArrayIndexOutOfBoundsException
。
if (myArray == null) {
System.out.println("myArray is null.");
} else {
System.out.println("myArray is not null.");
}
给myArray is not null.
。
所以我看不出为什么int[] myArray = new int[0]
应该优先选择的原因myArray = null;
。
这只是为了减少空检查。
您可以在空数组上进行迭代,但不能在null上进行迭代。
考虑一下代码:
for (Integer i: myArray) {
System.out.println(i);
}
在空数组上不打印任何内容,在null上导致NullPointerException
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句