String isEmpty() vs String isBlank() in Java

Author: SAI K


Introduction

In Java, the String class provides multiple methods to check if a string is empty or consists only of whitespace. Two commonly used methods for this purpose are isEmpty() and isBlank(). Although they might seem similar, they serve different purposes and have distinct behavior. This blog post will explore the differences between isEmpty() and isBlank().

Table of Contents

1. Understanding isEmpty()

The isEmpty() method checks if a string has a length of 0. It returns true if the string is empty and false otherwise.

Syntax:

public boolean isEmpty()

Example:

public class IsEmptyExample {
    public static void main(String[] args) {
        String str1 = "";
        String str2 = " ";
        String str3 = "Hello";

        System.out.println("str1 is empty: " + str1.isEmpty()); // true
        System.out.println("str2 is empty: " + str2.isEmpty()); // false
        System.out.println("str3 is empty: " + str3.isEmpty()); // false
    }
}

Output:

str1 is empty: true
str2 is empty: false
str3 is empty: false

2. Understanding isBlank()

The isBlank() method checks if a string is empty or consists only of whitespace characters. It returns true if the string is empty or contains only whitespace characters, and false otherwise.

Syntax:

public boolean isBlank()

Example:

public class IsBlankExample {
    public static void main(String[] args) {
        String str1 = "";
        String str2 = " ";
        String str3 = "\n\t";
        String str4 = "Hello";

        System.out.println("str1 is blank: " + str1.isBlank()); // true
        System.out.println("str2 is blank: " + str2.isBlank()); // true
        System.out.println("str3 is blank: " + str3.isBlank()); // true
        System.out.println("str4 is blank: " + str4.isBlank()); // false
    }
}

Output:

str1 is blank: true
str2 is blank: true
str3 is blank: true
str4 is blank: false

3. Differences Between isEmpty() and isBlank()

4. Usage Examples

Example Using isEmpty():

public class IsEmptyUsage {
    public static void main(String[] args) {
        String input = "   ";

        if (input.isEmpty()) {
            System.out.println("The input string is empty.");
        } else {
            System.out.println("The input string is not empty.");
        }
    }
}

Example Using isBlank():

public class IsBlankUsage {
    public static void main(String[] args) {
        String input = "   ";

        if (input.isBlank()) {
            System.out.println("The input string is blank.");
        } else {
            System.out.println("The input string is not blank.");
        }
    }
}

Output for Both Examples:

The input string is not empty.
The input string is blank.

5. Complete Example Program

public class StringCheckExample {
    public static void main(String[] args) {
        String str1 = "";
        String str2 = " ";
        String str3 = "Hello";
        String str4 = "\t\n";

        System.out.println("Using isEmpty() method:");
        System.out.println("str1 is empty: " + str1.isEmpty());
        System.out.println("str2 is empty: " + str2.isEmpty());
        System.out.println("str3 is empty: " + str3.isEmpty());
        System.out.println("str4 is empty: " + str4.isEmpty());

        System.out.println("\nUsing isBlank() method:");
        System.out.println("str1 is blank: " + str1.isBlank());
        System.out.println("str2 is blank: " + str2.isBlank());
        System.out.println("str3 is blank: " + str3.isBlank());
        System.out.println("str4 is blank: " + str4.isBlank());
    }
}

Output:

Using isEmpty() method:
str1 is empty: true
str2 is empty: false
str3 is empty: false
str4 is empty: false

Using isBlank() method:
str1 is blank: true
str2 is blank: true
str3 is blank: false
str4 is blank: true

6. Conclusion

The isEmpty() and isBlank() methods in Java are useful for checking whether a string is empty or contains only whitespace characters. While isEmpty() checks if the string's length is 0, isBlank() checks if the string is empty or consists only of whitespace characters. Understanding the differences between these methods helps in choosing the right method for various use cases.

Happy coding!

Related Java and Advanced Java Tutorials/Guides