StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.

From the linked documentation:

Checks if a String is whitespace, empty ("") or null.

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true  
 StringUtils.isBlank(" ")       = true  
 StringUtils.isBlank("bob")     = false  
 StringUtils.isBlank("  bob  ") = false

For comparison StringUtils.isEmpty:

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true  
 StringUtils.isEmpty(" ")       = false  
 StringUtils.isEmpty("bob")     = false  
 StringUtils.isEmpty("  bob  ") = false

Warning: In java.lang.String.isBlank() and java.lang.String.isEmpty() work the same except they don't return true for null.

java.lang.String.isBlank() (since Java 11)

java.lang.String.isEmpty()

Answer from arshajii on Stack Overflow
🌐
Educative
educative.io › answers › what-is-stringutilsisempty-in-java
What is StringUtils.isEmpty in Java?
isEmpty() is a static method of ... the string is considered to be empty. The length of the string is zero. The string points to a null reference....
🌐
Vultr Docs
docs.vultr.com › java › examples › check-if-a-string-is-empty-or-null
Java Program to Check if a String is Empty or Null | Vultr Docs
December 17, 2024 - Use StringUtils.isEmpty() to check ... StringUtils.isEmpty(str); } Explain Code · StringUtils.isEmpty() internally checks for null and empty strings, making your code cleaner and more expressive....
🌐
Reddit
reddit.com › r/learnjava › best way to check for null on strings?
Best way to check for null on Strings? : r/learnjava
May 8, 2024 - Plus "someString == null" will always be false since the result of String.valueOf will never be null. edit after reading josephblade's answer : +1 on the "optional is probably not what you want". [deleted] • 2y ago • Edited 2y ago · I think StringUtil.isEmpty will check if it's "" or null.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-2.6 › org › apache › commons › lang › StringUtils.html
StringUtils (Commons Lang 2.6 API)
Checks if a String is not empty ("") and not null. StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true
🌐
DEV Community
dev.to › niharmore33 › comment › 8hk
You should leverage StringUtils.isEmpty(str), which checks for empty strings ... - DEV Community
You should leverage StringUtils.isEmpty(str), which checks for empty strings and handles null gracefully.
🌐
iO Flood
ioflood.com › blog › stringutils-isempty
StringUtils.isEmpty() in Java: Your Method Guide
March 27, 2024 - At its core, StringUtils.isEmpty is a simple yet powerful tool in Java for checking if a string is empty or null.
Find elsewhere
Top answer
1 of 3
2

I feel like the premise of this question is already an incorrect one. You should not have to check for null strings in most places -- in fact, I would argue that you should avoid using null whenever possible, but especially when another non-null sentinel value exists. And String already has a very good "empty" value: the empty string ("")!

If "" and " " need to be folded into the same value, then there's already a perfectly good method for that in the standard library: .trim(). However, .trim(), being an instance method on String, only works on non-null strings. This isn't necessarily a bad thing!

If null and "" mean different things for you, then I would argue that your data model is too complex, and you should be using some other wrapper class rather than using String directly. If null and "" mean the same thing, then you should pick one or the other, and use it consistently. That may mean the need for a few != null checks, but if you find yourself needing an isNullOrEmpty or isNotBlank helper function frequently throughout your codebase, I would say that that's a code smell and you really should work on fixing the underlying data model issues rather than worrying about a tiny helper function.

What does that mean? In the Avoiding != null statements question, the top-voted answer points out that there are really only two kinds of instances where a value can be null: either (1) null is a valid value, or (2) null isn't a valid value.

Case (2) isn't very interesting. Null is not a valid value, so we shouldn't try to deal with it. If anything, we simply throw an exception if we encounter it. Otherwise we ignore it, and let a NullPointerException happen "naturally". It's not supposed to be null, so by definition finding a null is an exceptional situation.

If null is a valid value, then it means null has a semantic meaning. Most likely it means that a value is "not present" or "not valid". There are two sub-cases here: (1a) null means the same thing as the empty string, or (1b) it means something different.

If you have case (1b), then I would argue that you need a new entity in your domain model. For example, you could create a class like PaymentTerm which has separate .isValid() and .isPresent() methods, as well as a .asString() accessor to get the string value if it is present. (The are a lot of possible ways to make a PaymentTerm class, with lots of possible tradeoffs: the point is not that you need this specific form, but that you need something more than a raw String, something that you can hang methods off of, because this thing is now a first-class entity in your domain model.)

If you have case (1a), then null and the empty string both mean the same thing, semantically. But they are very different syntactically! The empty string already has an instance method to check for it (.isEmpty()) and can be safely stored, passed around, compared to other strings, etc.

So case (1a) has two possible solutions: (1a.1) you pass around both null and the empty string, and everywhere you have to check for either, or (1a.2) you normalize nulls to empty strings at the soonest opportunity, and then treat null as an invalid value, and use the empty string everywhere. Depending on your input format, you may even get this behavior "for free" (e.g. an empty text box naturally has the empty string as a value, not null).

My argument is that case (1a.1) is a code smell. Rather than passing around both null and the empty string, and frequently checking for both (either manually or with a method like isNullOrEmpty or isNotBlank), you should try to move into case (2) or case (1a.2).

Note that this answer actually implies that both isNotBlank and != null are sub-optimal! In a well-factored codebase you should strive to avoid both of them, but I tend to think that you should strive to avoid something like isNotBlank even more.

That said, how you check for null or the empty string is not terribly important. The JIT will almost certainly inline the checks anyway, and in many cases the peephole optimizer will elide them completely if it can prove null-safety another way. The far more important thing to consider is whether null is a valid value in your program, and if so, what it means semantically for a value to be null.

2 of 3
1

paymentTerm != null and StringUtils.isNotBlank(paymentTerm) don't perform the same thing.
If you want to only check the non nullity of a String object, you don't want to use isNotBlank() but use != null. So it may still make sense to use != null.
Note that as alternative you could also use Object.nonNull(Object) but it is often more verbose (but in a method reference where it makes completely sense : Object::nonNull).

After about whether we should test != "", != trimmed "" or just != null, it is a requirement matter.
Even if isNotBlank() includes != null, you will use it only as it is required because performing more checks that required is misleading code.


Here are simple examples where you can see that each way has a meaning and you have to use them (or no of them) in a way what makes their reading natural and pleasant.

1) You want to check the String length size.

if (myString != null && myString.length() == 8)

is what you need.
Doing it with isNotBlank() is verbose and conveys two very specific things (not blank and min length) while only the last one matters.

if (StringUtils.isNotBlank(myString) && myString.length() == 8)

2) You want to check that the String contain some characters.

if (myString != null && myString.contains("word"))

is still what you need.

if (String.isNotBlank(myString) && myString.contains("word"))  

Doing it with isNotBlank() appears still as noise.

3) You want to check the String equal to another one that cannot be null or that is a compile-time constant expression.

You want to directly write :

if ("word".equals(myString))

4) So when do you want to use isNotBlank() ?
Only when you need to check that a String is not null and doesn't contain only whitespaces (" ") characters.

if (StringUtils.isNotBlank("word"))
🌐
DEV Community
dev.to › monknomo › finding-null-or-empty-string-checks-in-java › comments
[Discussion] Finding Null or Empty String Checks in Java — DEV Community
I have a lot of code like this: if( null == myString || "".equals(myString)) doAThing(); Enter fullscreen mode Exit fullscr... ... You should leverage StringUtils.isEmpty(str), which checks for empty strings and handles null gracefully.
🌐
Kodejava
kodejava.org › how-do-i-check-for-an-empty-string
How do I check for an empty string? - Learn Java by Examples
This method will return true if // the tested string is empty, contains whitespaces only or null. System.out.println("Is one empty? " + StringUtils.isBlank(one)); System.out.println("Is two empty?
🌐
TutorialsPoint
tutorialspoint.com › Checking-for-Null-or-Empty-in-Java
Checking for Null or Empty in Java.
The following is an example of checking if a string is null or empty using the isBlank() method: import org.apache.commons.lang3.StringUtils; public class CheckStringBlank { public static void main(String[] args) { String input = " "; if (StringUtils.isBlank(input)) { System.out.println("The ...
🌐
Java67
java67.com › 2014 › 09 › right-way-to-check-if-string-is-empty.html
Right way to check if String is empty in Java with Example | Java67
The only caveat here is that this method returns true in the case of null input as well, which may not be correct depending upon your application's definition of empty String. If you treat null as an empty string then you can use this wonderful ...
🌐
Educative
educative.io › answers › what-is-stringutilsisnotempty-in-java
What is StringUtils.isNotEmpty in Java?
isNotEmpty() is a static method of the StringUtils class that is used to check if the given string is not empty. If a string does not satisfy any of the criteria below, then the string is considered to be empty. The length of the string is zero. The string points to a null reference...
🌐
Buggybread
buggybread.com › 2014 › 02 › java-difference-between.html
Java - Difference between StringUtils.EMPTY and StringUtils.isEmpty()
StringUtils.isEmpty() Checks if a String is empty ("") or null. How does StringUtils.EMPTY.equals(string) different from StringUtils.isEmpty(string) ... Though StringUtils.isEmpty(str2) is saying that its checking for empty str2 but its actually checking both null as well as blank string for str2.
🌐
Stack Abuse
stackabuse.com › java-check-if-string-is-null-empty-or-blank
Java: Check if String is Null, Empty or Blank
February 28, 2023 - String string = "Hello there"; if (string == null || string.equals("") || string.trim().equals("")) System.out.println("String is null, empty or blank"); else System.out.println("String is neither null, empty nor blank"); In much the same fashion as the before, if the trimmed string is "", it was either empty from the get-go, or was a blank string with 0..n whitespaces: ... The Apache Commons is a popular Java library that provides further functionality. StringUtils is one of the classes that Apache Commons offers.
🌐
Medium
tamerardal.medium.com › simplify-null-checks-in-java-writing-clean-code-with-apache-commons-lang-3-e7d3aea207bd
Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3 | by Tamer Ardal | Medium
September 18, 2024 - Especially when working with String values, we need to do a lot of null or empty checking. In this case, we can easily do this with the StringUtils class. if (StringUtils.isBlank(myString)) { // String is null, empty or contains only spaces } if (StringUtils.isNotBlank(myString)) { // String contains a valid value } ... if (StringUtils.isEmpty(myString)) { // String null or empty } if (StringUtils.isNotEmpty(myString)) { // String contains a valid value (Can contain only spaces) }