Explain Codes LogoExplain Codes Logo

Check whether a String is not Null and not Empty

java
string-checks
best-practices
null-safety
Alex KataevbyAlex Kataev·Aug 11, 2024
TLDR

To verify a String is not null and not empty using the && operator alongside Java's isEmpty() method:

if (myString != null && !myString.isEmpty()) { // Who's a good string? You are! }

Make certain myString is neither null nor hollow before following up with your program logic.

In order to cover all bases, we need to dive deep into the finer details and best practices pertinent to string validation in Java.

The art and science of whitespace checking

Trimming the fluff

If whitespace characters are considered Empty, use trim() before isEmpty():

if (myString != null && !myString.trim().isEmpty()) { // String has substance, not just hot air! }

The Blank new world of Java 11+

From Java 11 onwards, life is easy with isBlank(), checking both whitespace and emptiness:

if (myString != null && !myString.isBlank()) { // No white lies here, the string is legit! }

The tale of length and libraries

Old school length check

Before isEmpty() swaggered in, the norm was to check the length of the string:

if (myString != null && myString.length() > 0) { // String has enough characters for a party! }

Leveraging libraries wisely

The Apache Commons Lang and Google Guava libraries are like magic wands bestowing superpowers:

// This is how you do it with Apache Commons Lang if (StringUtils.isNotBlank(myString)) { // The string is full of life, not null, not empty, and not a lazy whitespace! } // Google Guava style if (!Strings.isNullOrEmpty(myString)) { // String is alive and kicking, neither null nor empty! }

Use external libraries with caution - ensure they are part of your project's arsenal of dependencies.

Good practices for safe string checks

Short-circuiting to save the day

Prevent NullPointerException from ruining your day using short-circuit evaluation :

if (myString != null && !myString.isEmpty()) { // Second condition is only evaluated if myString isn't a ghost (null)! }

Maintainability and reusability

Spruce up code quality by building custom validation methods for string checks:

public static boolean isNotNullOrEmpty(String str) { return str != null && !str.isEmpty(); }

Now you can call:

if (isNotNullOrEmpty(myString)) { // String passed the sanity check! }

Contextualization and validation of strings

SQL/HQL scenarios

In scenarios involving SQL or HQL, remember that null and empty strings could potentially represent unique states, like Schroedinger's cat.

Dealing with Unicode in isBlank

From Java 11 onwards, isBlank() is a no-nonsense, sophisticated method that treats all Unicode whitespace characters as empty.

Normalize for safety

Normalize string inputs early in the application cycle to avoid heartburn at a later stage.

Best practices for various Java versions

Pre-Java 11 compatibility tips

In the times before Java 11, use isEmpty() along with manual whitespace checks:

if (myString != null && !myString.trim().isEmpty()) { // String is not null, empty, and not a white lie! }

Android best practices

In the realm of Android development, the TextUtils class offers its own useful methods:

if (!TextUtils.isEmpty(myString)) { // String is not null and not empty - all systems go! }