8

I want to know the best practice for checking whether a string array is empty or not.

String[] name = {"a" , "b"};

if (name == null) {
}

Is this a good practice or there are more best codes for the same?

2
  • 1
    What do you mean by "boolean value of the String[]?" What do you expect to be true, what do you expect to be false? Commented Mar 4, 2014 at 3:28
  • I think the concept of a "boolean value of String[]" simply doesn't apply in Java. It's not like JavaScript where things are either truthy or falsy. You can check to see whether a reference is null, or whether the elements of an array are null, but that's about all. Commented Mar 4, 2014 at 3:31

4 Answers 4

25

Normally you would want to do something like:

if (arr != null && arr.length > 0) { ... }

for non-empty array.

However, as you may suspect, someone have made utils for such kind of common action. For example, in Commons-lang, you can do something like:

if (ArrayUtils.isEmpty(arr)) {... }

if you do a static import for ArrayUtils.isEmpty, this line can be even shorter and looks nicer:

if (isEmpty(arr)) { ... }
Sign up to request clarification or add additional context in comments.

2 Comments

org.apache.commons.lang.ArrayUtils this will be more useful IF THE PROJECT IS ALREADY USING apache package , normally simple prog dont use that jar distribution
@SrinathGanesh of course whether to use 3rd party lib is the choice of OP but, with modern build tools like Maven and Gradle, I don't see any problem using such lib in simple programs.
6
if(name!=null && name.length > 0) {
   // This means there are some elements inside name array.
} else {
   // There are no elements inside it.
}

6 Comments

length in an array is a field, not a method.
@BrianRoach, thanks for pointing out, by mistake I added parenthesis.
is nullcheck required ?? length already does the job
@SrinathGanesh, Yes null check is required, coz if name is null, then name.length will give NullPointerException
is checking length required ? not null is not suffice ??
|
2

To check if a string array is empty...

public boolean isEmptyStringArray(String [] array){
 for(int i=0; i<array.length; i++){ 
  if(array[i]!=null){
   return false;
  }
  }
  return true;
}

Comments

2

All arrays in Java have a special field "length" that contains the number of elements in the array, that is array length in other words.

String test( String[] array )
{
    if ( array == null ) {
        return "array is null";
    }
    if ( array.length == 0 ) {
        return "array is empty, meaning it has no element";
    }
    for ( String s : array ) {
        if (s == null) {
            return "array contains null element";
        }
        if (s.length() == 0) {
            return "array contains empty string";
        }
        // or
        if (s.isEmpty()) {
            return "array contains empty string";
        }
    }

    return "array is not null or empty and does not contain null or empty strings";
}

To test whether the array contains a null element or an empty string you need to iterate through it and check each element individually.

Do not forget that the length of array is the special field array.legnth and the length of the string is the function string.length().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.