9/08/2006

Is a String Empty?

Prior to JDK 6, we can check if a string is empty in 2 ways:

  • if(s != null && s.length() == 0)
  • if(("").equals(s))
Checking its length is more readable and may be a little faster. Starting from JDK 6, String class has a new convenience method isEmpty():
boolean isEmpty()
Returns true if, and only if, length() is 0.
It is just a shorthand for checking length. Of course, if the String is null, you will still get NullPointerException. I don't see much value in adding this convenience method. Instead, I'd like to see a static utility method that also handle null value:
public static boolean notEmpty(String s) {
return (s != null && s.length() > 0);
}

15 comments:

Thorbjørn Ravn Andersen said...

It is most likely to make it easier to check from e.g JSF expression language, where it is easy to get to bean methods but hard to get to calling static methods with arguments.

Anonymous said...

I would prefer isEmpty, because then you can read your source in a natural way.

Deepak Singhvi said...

better to use StringUtils.isEmpty(String str) of Apache commons , can be downloaded from

http://commons.apache.org/


public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}

SoccerHero said...

Why is StringUtils empty better? Does it handle null pointers?

Anonymous said...

Yes, that's what "str == null" tests; and as the || operator won't evaluate its 2nd argument if the first is true you won't access the null pointer with str.length().

D. Veloper said...

You also have to consider checking for whitespaces, if your definition of "empty string" doesn't match a string like " ".

For Java 6+:

public static boolean isEmptyOrBlank(String str) {
return str == null || str.trim().isEmpty();
}

Anonymous said...

THANK YOU!!!!

Anonymous said...

THANK U MADRCHOOD

Anonymous said...

Thanks

Anonymous said...

very helpful. Thanks!!

Fred Ward said...

Thank you D. Veloper. You solved my problem.

Java programmer said...

Here is couple of more ways to check if String is Empty in Java

Jamison Gray said...

Very late followup: note that D. Veloper's solution for handling whitespace-only strings, containing "str.trim().isEmpty()", allocates and builds an entire new String object, copying out the trimmed portion of the source string (probably building up a StringBuffer internally in the process, before copying that out into an immutable String). This would be less than ideal in an inner loop, especially if the normal case is for strings to non-empty. You'll be creating a lot of work for your garbage collector.

It would be better to avoid the string copies. A regular expression using the Java6 Pattern object could work (with the Pattern compiled outside the loop), or just iterating along the string using charAt() with a simple "for" loop.

The Guava (com.google.common) toolkit has a nifty CharMatcher class that should help. This ought to do it:

CharMatcher.WHITESPACE.matchesAllOf(str)

Of course, it would be good to test out the performance implications of whatever solution you try; it's possible that these other approaches might contain hidden inner work that is is greater than str.trim().isEmpty().

Jamison Gray said...

I got curious, and looked at the source for String.trim():

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.trim%28%29

It's pretty efficient and doesn't do an intermediate StringBuffer, but there's still a fresh String object allocated and returned. Its code points toward the simple loop one could write to see if there are any non-blanks in a string.

lee woo said...

The purpose of education is to replace an empty mind with an open one. See the link below for more info.

#empty
www.mocsbar.com