7

The javadoc of ArrayUtils.isNotEmpty() in Apache Commons Lang seems to be wrong. Or, at least, misleading. It says

Returns: true if the array is not empty or not null

In my understanding, an empty array is not null. So, according to the above definition, isNotEmpty() should return true for an empty array, which is counterintuitive.

Wouldn't

Returns: true if the array is not null and not empty

be more correct?

3
  • Are you going to say which ArrayUtils are you talking about? There is no such class in Java API. Commented Jan 21, 2014 at 9:49
  • Sorry, forgot to say. It's ArrayUtils from org.apache.commons.lang, both version 2.6 and 3.2.1 Commented Jan 21, 2014 at 15:34
  • Seems like it was an issue only until 3.4 Has been fixed since 3.5 Commented Oct 21, 2019 at 18:19

2 Answers 2

5

Wouldn't

Returns: true if the array is not null and not empty

be more correct?

Yes you are right. The doc is a bit misleading. In fact, if you see the source code, it does exactly that:

public static boolean isNotEmpty(Object[] array) {
   return (array != null && array.length != 0);
}
Sign up to request clarification or add additional context in comments.

Comments

0

In my understanding, an empty array is not null.

Not correct. Counterexample:

int a[];

a is an empty array (as it doesn't contain anything), and it's also null since it wasn't initialized.

In this case, isNotEmpty will return false, since it is empty.

7 Comments

Sorry but I've to completely disagree with your empty array point. int[] a is not an empty array, because there is no array at all.
@RohitJain If you pass a to a method that expects an array, it'll be fine.. So what is a? Just a reference?
Yeah, but that reference should point to an array, which should not be empty. Now, as it is pointing to a non-empty array, it cannot be null. Frankly speaking, OP is right. The doc comment is wrong, and not consistent with the code. See my answer.
@RohitJain Thanks for your remark, I'll further investigate this.
@RohitJain No int a[]; and String b; are not initialized to null. Simply aren´t initialized, if you try to use them you will get a compiler error 'The local variable a may not have been initialized'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.