There's a key difference between a null array and an empty array. This is a test for null.
int arr[] = null;
if (arr == null) {
System.out.println("array is null");
}
"Empty" here has no official meaning. I'm choosing to define empty as having 0 elements:
arr = new int[0];
if (arr.length == 0) {
System.out.println("array is empty");
}
An alternative definition of "empty" is if all the elements are null:
Object arr[] = new Object[10];
boolean empty = true;
for (int i=0; i<arr.length; i++) {
if (arr[i] != null) {
empty = false;
break;
}
}
or
Object arr[] = new Object[10];
boolean empty = true;
for (Object ob : arr) {
if (ob != null) {
empty = false;
break;
}
}
Answer from cletus on Stack OverflowThere's a key difference between a null array and an empty array. This is a test for null.
int arr[] = null;
if (arr == null) {
System.out.println("array is null");
}
"Empty" here has no official meaning. I'm choosing to define empty as having 0 elements:
arr = new int[0];
if (arr.length == 0) {
System.out.println("array is empty");
}
An alternative definition of "empty" is if all the elements are null:
Object arr[] = new Object[10];
boolean empty = true;
for (int i=0; i<arr.length; i++) {
if (arr[i] != null) {
empty = false;
break;
}
}
or
Object arr[] = new Object[10];
boolean empty = true;
for (Object ob : arr) {
if (ob != null) {
empty = false;
break;
}
}
ArrayUtils.isNotEmpty(testArrayName) from the package org.apache.commons.lang3 ensures Array is not null or empty
I recently learnt that an empty array IN JAVA is filled with "0" not null and I was wondering how you will be able to determine the size of the array if you cant say "i != null " how can you tell how many elements are in the array?
Technically speaking, there's no such thing as a null array; but since arrays are objects, array types are reference types (that is: array variables just hold references to arrays), and this means that an array variable can be null rather than actually pointing to an array:
int[] notAnArray = null;
An empty array is an array of length zero; it has no elements:
int[] emptyArray = new int[0];
(and can never have elements, because an array's length never changes after it's created).
When you create a non-empty array without specifying values for its elements, they default to zero-like values — 0 for an integer array, null for an array of an object type, etc.; so, this:
int[] arrayOfThreeZeroes = new int[3];
is the same as this:
int[] arrayOfThreeZeroes = { 0, 0, 0 };
(though these values can be re-assigned afterward; the array's length cannot change, but its elements can change).
An array has its members initialized to their default values. For int the default is 0. For an Object it's null. A null array is a null Array Reference (since arrays are reference types in Java).
JLS-4.12.5 Initial Values of Variables says in part
For type int, the default value is zero, that is, 0.
and
For all reference types (§4.3), the default value is null.
Because .size() is method, its implementation is hidden. And you can't be sure that this method optimized (there's a possibility, that some collections, for example, enumerate items every time you call .size() method). That is why computing the .size() of collection could be expensive. In the same time, .isEmpty() can checks just is there at least one item in collection, and return result.
But .length is just a field. It does nothing, just returns a value. There are no calculations. This field just stores the length of array with fixed size.
I believe that .size() and .length are totally different things and in this case they can not be compared, as well as collections and arrays.
The easiest way would to be to write such a method yourself. It would be fairly trivial. Something like the following
public boolean isArrayEmpty(Object[] array) {
if (array == null) {
//Throw nullpointer exception
//An easy of method of doing this would be accessing any array field (for example: array.length)
}
return (ArrayUtils.isEmpty(array));
}
That being said, there's very little reason why array.length == 0 would be harmful. It's the simplest, cleanest way to do this and does exactly what you want. Most likely part of the reason for ArrayUtils.isEmpty is to allow easy testing of whether or not a array is empty or null, a relatively common need, but you don't want to do that.
You're trying to call the isEmpty() method on a null reference (as List test = null;). This will surely throw a NullPointerException. You should do if(test!=null) instead (checking for null first).
The method isEmpty() returns true, if an ArrayList object contains no elements; false otherwise (for that the List must first be instantiated that is in your case is null).
You may want to see this question.
I would recommend using Apache Commons Collections:
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/CollectionUtils.html#isEmpty-java.util.Collection-
which implements it quite ok and well documented:
/**
* Null-safe check if the specified collection is empty.
* <p>
* Null returns true.
*
* @param coll the collection to check, may be null
* @return true if empty or null
* @since Commons Collections 3.2
*/
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}