String isEmpty() vs String isBlank() in Java

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()
  2. Understanding isBlank()
  3. Differences Between isEmpty() and isBlank()
  4. Usage Examples
  5. Complete Example Program
  6. Conclusion

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

Explanation:

  • str1.isEmpty() returns true because str1 is an empty string with a length of 0.
  • str2.isEmpty() returns false because str2 contains a space character and has a length of 1.
  • str3.isEmpty() returns false because str3 contains the word "Hello" and has a length greater than 0.

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

Explanation:

  • str1.isBlank() returns true because str1 is an empty string.
  • str2.isBlank() returns true because str2 contains only a space character.
  • str3.isBlank() returns true because str3 contains only whitespace characters (newline and tab).
  • str4.isBlank() returns false because str4 contains the word "Hello" and is not empty or whitespace only.

3. Differences Between isEmpty() and isBlank()

  • Whitespace Characters:

    • isEmpty() returns false if the string contains any characters, including whitespace.
    • isBlank() returns true if the string contains only whitespace characters or is empty.
  • Method Introduction:

    • isEmpty() has been available since Java 6.
    • isBlank() was introduced in Java 11.

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.

Explanation:

  • In the isEmpty() example, the string " " is not considered empty because it contains spaces.
  • In the isBlank() example, the string " " is considered blank because it contains only whitespace characters.

5. Complete Example Program

Here is a complete program that demonstrates the differences between isEmpty() and isBlank().

Example Code:

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!

Comments