4

I have an array of objects (Object[] array), and I want to check whether it is empty, but fail fast if it is null with NPE or any other exception.

I know that I can do the following check

array.length == 0

but e.g. in case of String or collections it is not recommended practice, IntelliJ even has inspection for that:

Reports any .size() or .length() comparisons with a 0 literal which can be replaced with a call to .isEmpty().

Is there any replacement for .isEmpty() for arrays?

There is ArrayUtils.html#isEmpty(java.lang.Object[]), but it also checks for null, while I'd like to avoid that.

8
  • 3
    It would be simple to write the desired method yourself. Commented Feb 16, 2016 at 12:52
  • what about getLength() from java.lang.Object Commented Feb 16, 2016 at 12:54
  • Strings and collections are objects, so isEmpty() makes sense. An array of whatever, even objects, is just that, an array. It contains no "smarts", sof if you need smarts, you need to add it yourself - it does make them super lightweight though. Commented Feb 16, 2016 at 12:55
  • @Jesper it must be the fun to ask questions on SO Commented Feb 16, 2016 at 12:55
  • use array.length == 0 and ignore intellijs useless tip, because isEmpty() obviously doesn't do what you want. be sure to check for null first Commented Feb 16, 2016 at 12:56

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

Why throw your own NullPointerException? Just do return array.length == 0. And ArrayUtils is not native Java
@cricket_007 That would be one way of doing it. I don't know what the op wants to do. But I'll add that to the answer as a suggestion as a possible method of doing it. Thanks for your input!
returning ArrayUtils.isEmpty(array) is a very roundabout way of returning array.length == 0. I would rather fix IntelliJ's idea of what is good or bad rather than using ArrayUtils for this task

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.