Why Even Experienced Java Developers Misunderstand isEmpty vs. isBlank
This article explains the precise differences between Apache Commons Lang's StringUtils.isEmpty, isBlank, and their related methods, providing concrete examples, code snippets, and usage guidelines to help developers avoid common pitfalls when checking string emptiness in Java.
A new colleague with three years of Java experience still confuses the behavior of isEmpty and isBlank, prompting a detailed walkthrough of the org.apache.commons.lang3.StringUtils utility class.
isEmpty series
StringUtils.isEmpty()
Checks whether a CharSequence is null or has a length of zero. A single space character is **not** considered empty, so isEmpty(" ") returns false.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
/* NOTE: This method changed in Lang version 2.0.
It no longer trims the CharSequence. That functionality is now available in isBlank(). */
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}StringUtils.isNotEmpty()
Simply the negation of isEmpty().
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}StringUtils.isAnyEmpty()
Returns true if **any** of the supplied CharSequence arguments is null or empty.
StringUtils.isAnyEmpty(null) = true
StringUtils.isAnyEmpty(null, "foo") = true
StringUtils.isAnyEmpty("", "bar") = true
StringUtils.isAnyEmpty("bob", "") = true
StringUtils.isAnyEmpty(" bob ", null) = true
StringUtils.isAnyEmpty(" ", "bar") = false
StringUtils.isAnyEmpty("foo", "bar") = false
public static boolean isAnyEmpty(final CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return true;
}
for (final CharSequence cs : css) {
if (isEmpty(cs)) {
return true;
}
}
return false;
}StringUtils.isNoneEmpty()
Equivalent to !isAnyEmpty(css); returns true only when **all** arguments are non‑null and non‑empty.
StringUtils.isNoneEmpty(null) = false
StringUtils.isNoneEmpty(null, "foo") = false
StringUtils.isNoneEmpty("", "bar") = false
StringUtils.isNoneEmpty("bob", "") = false
StringUtils.isNoneEmpty(" bob ", null) = false
StringUtils.isNoneEmpty(" ", "bar") = true
StringUtils.isNoneEmpty("foo", "bar") = true
public static boolean isNoneEmpty(final CharSequence... css) {
return !isAnyEmpty(css);
}isBlank series
StringUtils.isBlank()
Considers null, the empty string, and strings consisting solely of whitespace as "blank".
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
public static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}StringUtils.isNotBlank()
Negates isBlank().
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}StringUtils.isAnyBlank()
Returns true if **any** of the supplied arguments is blank (null, empty, or whitespace only).
StringUtils.isAnyBlank(null) = true
StringUtils.isAnyBlank(null, "foo") = true
StringUtils.isAnyBlank(null, null) = true
StringUtils.isAnyBlank("", "bar") = true
StringUtils.isAnyBlank("bob", "") = true
StringUtils.isAnyBlank(" bob ", null) = true
StringUtils.isAnyBlank(" ", "bar") = true
StringUtils.isAnyBlank("foo", "bar") = false
public static boolean isAnyBlank(final CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return true;
}
for (final CharSequence cs : css) {
if (isBlank(cs)) {
return true;
}
}
return false;
}StringUtils.isNoneBlank()
Returns true only when **none** of the arguments are blank.
StringUtils.isNoneBlank(null) = false
StringUtils.isNoneBlank(null, "foo") = false
StringUtils.isNoneBlank(null, null) = false
StringUtils.isNoneBlank("", "bar") = false
StringUtils.isNoneBlank("bob", "") = false
StringUtils.isNoneBlank(" bob ", null) = false
StringUtils.isNoneBlank(" ", "bar") = false
StringUtils.isNoneBlank("foo", "bar") = true
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}Other StringUtils methods
For a complete list of useful utilities, refer to the official Apache Commons Lang documentation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
SpringMeng
Focused on software development, sharing source code and tutorials for various systems.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
