Java String isEmpty() Method with Examples

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

The isEmpty() method in Java is a valuable tool for efficiently checking if a string is empty or contains characters. In this article, we will delve into the syntax and practical usage of this method through illustrative examples.

The isEmpty() method is found in the java.lang.String class was introduced in Java 1.6. It offers a convenient means to determine whether a string is empty or not. An empty string, for clarification, is one with a length of 0, and this method yields a ‘true’ result for empty strings and ‘false’ for non-empty ones.

Syntax and Return Value of Java isEmpty() Method

The syntax of Java isEmpty() method is:

boolean isEmpty()

It returns:

  • true – if the string is empty (length is 0)
  • false – if the string is not empty (length > 0)

Example Usage

Let’s see some examples to understand the usage of isEmpty() method better.

Example 1:

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

        System.out.println(str1.isEmpty()); // true
        System.out.println(str2.isEmpty()); // false
    }
}

Output:
true
false

In this example, str1 is an empty string, so isEmpty() returns true. For str2, the length is greater than 0, so it returns false.

Example 2:

This example shows the difference between empty and blank strings:

public class EmptyAndBlankStringsExample {
    public static void main(String[] args) {
        String empty = "";
        String blank = " ";

        System.out.println(empty.isEmpty()); // true
        System.out.println(blank.isEmpty()); // false

        System.out.println(empty.length()); // 0
        System.out.println(blank.length()); // 1
    }
}

Output:
true
false
0
1

Frequently Asked Questions

Q1. Are empty strings and null strings the same?

No, empty and null strings are not the same in Java. An empty string is a valid string object that has 0 length. A null string is a string variable that doesn’t refer to any string object, it points to null.

Q2. What is a blank string?

A blank string refers to a string that contains only whitespace characters like spaces, tabs etc. It is not empty but contains whitespace characters.

Conclusion

The isEmpty() method is a valuable addition to Java’s string processing capabilities, simplifying the process of determining whether a string is empty or contains characters. Its ability to return a boolean value aids in the efficient validation of input strings. When combined with other String methods such as trim() and length(), isEmpty() contributes to robust string handling.

In summary, the isEmpty() method enhances code clarity and conciseness for empty string checks, making it a powerful asset in Java string manipulation.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

Leave a Reply

Your email address will not be published. Required fields are marked *