Is there a reason why
int[] myArray = new int[0];
compiles?
Is there any use of such an expression?
myArray[0] = 1;
gives java.lang.ArrayIndexOutOfBoundsException
.
if (myArray == null) {
System.out.println("myArray is null.");
} else {
System.out.println("myArray is not null.");
}
gives myArray is not null.
.
So I can't see a reason why int[] myArray = new int[0]
should be preferred over myArray = null;
.
It's just for reducing null checks.
You can iterate on empty array but can not iterate on null.
Consider the code:
for (Integer i: myArray) {
System.out.println(i);
}
On empty array it prints nothing, on null it causes NullPointerException
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With