Java Program to Check if a String is Empty or Null

Updated on December 17, 2024
Check if a string is empty or null header image

Introduction

In Java, checking whether a string is empty or null is a common operation, especially when dealing with user inputs or processing data where a string's presence needs to be validated. The Java language provides various ways to handle this, ensuring robustness and preventing runtime errors like NullPointerException from derailing your application.

In this article, you will learn how to effectively check if a string is empty or null using several methods provided by Java. Explore practical examples that showcase these techniques to help you integrate them into your Java applications seamlessly.

Basic Empty and Null Check

Check Using Standard If-Else

  1. Use the basic logical operations directly to check if a string is null or empty.

    java
    public static boolean isNullOrEmpty(String str) {
        if (str == null || str.isEmpty()) {
            return true;
        }
        return false;
    }
    

    This code checks if the variable str is null or empty (""). If either condition is true, it returns true; otherwise, it returns false.

Alternative Using Objects Class

  1. Utilize the Objects utility class for a null-safe check.

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

    Similar to the previous example, this function returns true if str is null or its length is zero. Using this one line enhances readability and reduces code complexity.

Leveraging Apache Commons Library

Using StringUtils.isEmpty()

  1. Understand that the Apache Commons Lang library provides enhanced string handling utilities.

  2. Add the Apache Commons Lang dependency to your project.

    For Maven users, include this in your pom.xml:

    xml
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
    </dependency>
    
  3. Use StringUtils.isEmpty() to check for null or empty strings.

    java
    import org.apache.commons.lang3.StringUtils;
    
    public static boolean isNullOrEmpty(String str) {
        return StringUtils.isEmpty(str);
    }
    

    StringUtils.isEmpty() internally checks for null and empty strings, making your code cleaner and more expressive.

Using StringUtils.isBlank()

  1. Understand the difference: isBlank() checks for null, empty, and whitespace only strings.

  2. Implement similar to isEmpty() but with isBlank() for a broader check.

    java
    import org.apache.commons.lang3.StringUtils;
    
    public static boolean isNullOrEmpty(String str) {
        return StringUtils.isBlank(str);
    }
    

    Here, StringUtils.isBlank() checks if str is null, empty, or made solely of whitespace (spaces, tabs, new line characters). Very useful when the mere presence of whitespace should also classify the string as "empty".

Java 11 and Beyond: Using String Methods

Using String.isBlank() in Java 11

  1. Recognize the enhancements in Java 11 including new String methods.

    java
    public static boolean isNullOrEmpty(String str) {
        return str == null || str.isBlank();
    }
    

    With Java 11, String.isBlank() does what StringUtils.isBlank() from Apache Commons does but without the need for an external library.

Conclusion

Checking if a string is empty or null in Java can be approached in multiple ways, each serving different needs depending on your project requirements. From simple conditional checks and utilizing built-in Java methods to employing third-party libraries for more nuanced checks, choose the right method that fits your context. These methods enhance robustness, prevent common errors, and improve the overall safety and readability of your Java code. Implement these techniques in different scenarios to maintain high-quality code standards in your Java projects.