English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)/O)

Java Reader/Writer

Java other topics

Java program to check if the string is null or empty

Java Examples Comprehensive

In this program, you will learn to use if-else statement and Java function to check if the string is empty or null.

Example1: Check if the string is empty or empty

public class Null {
    public static void main(String[] args) {}}
        String str1 = null;
        String str2 = "";
        if (isNullOrEmpty(str1))
            System.out.println("The first string is null or empty.");
        else
            System.out.println("The first string is not null or empty.");
        if (isNullOrEmpty(str2))
            System.out.println("The second string is null or empty.");
        else
            System.out.println("The second string is not null or empty.");
    }
    public static boolean isNullOrEmpty(String str) {
        if(str != null && !str.isEmpty())
            return false;
        return true;
    }
}

When running the program, the output is:

The first string is null or empty.
The second string is null or empty.

In the above program, we have two strings str1and str2.str1Contains null value, str2It is an empty string.

We also created a function isNullOrEmpty(), as the name suggests, which checks if the string is null or empty. It uses != null and the string's isEmpty() method for null checking.

In simple terms, if a string is not null and isEmpty() returns false, then it is neither null nor empty. Otherwise, it is.

However, if the string only contains whitespace characters (spaces), the above program will not return empty. Technically, isEmpty() finds that it contains spaces and returns false. For strings with spaces, we use the string method trim() to trim all leading and trailing whitespace characters.

Example2: Check if the string with spaces is empty or empty

public class Null {
    public static void main(String[] args) {}}
        String str1 = null;
        String str2 ="        ";
        if (isNullOrEmpty(str1))
            System.out.println("str1Is null or empty.");
        else
            System.out.println("str1Is not null or empty.")
        if (isNullOrEmpty(str2))
            System.out.println("str2Is null or empty.");
        else
            System.out.println("str2Is not null or empty.")
    }
    public static boolean isNullOrEmpty(String str) {
        if (str != null && !str.trim().isEmpty())
            return false;
        return true;
    }
}

When running the program, the output is:

str1Is null or empty.
str2 is null or empty.

In isNullorEmpty(), we added an additional method trim(), which can remove all leading and trailing space characters from the given string.

Therefore, now, if the string only contains spaces, the function will return true.

Java Examples Comprehensive