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);


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


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

Anonymous 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...


Anonymous said...


Anonymous said...


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:


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():


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.

Unknown said...

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


oppa said...

Interesting details you have remarked, thank you for putting this article together.


Unknown said...

Excellent and decent post. I found this much informative, as to what I was exactly searching for. Thanks for such post and keep it up.


Unknown said...

Love it! Very interesting topics, I hope the incoming comments and suggestion are equally positive. Thank you for sharing this information that is actually helpful.


Unknown said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Course in Chennai

Java Online Training India

eMexoT said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

Internet Of Things training in electronic city