Java String isEmpty()

The isEmpty() method in Java is a built-in method in the java.lang.String class that checks whether a string is empty or not. It returns a boolean value of true if the string is empty (contains no characters) and false otherwise.

Here is the syntax for the isEmpty() method:

public boolean isEmpty()

Here’s an example:

String str1 = "";
String str2 = "Hello";

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

In the above example, str1 is an empty string, so str1.isEmpty() returns true. str2, on the other hand, contains the string “Hello”, so str2.isEmpty() returns false.

Signature of Java String isEmpty():

The isEmpty() method in Java is a non-static method of the java.lang.String class that checks whether a string is empty or not. It has the following signature:

public boolean isEmpty()

The isEmpty() method takes no arguments and returns a boolean value indicating whether the string is empty (true) or not (false).

Here’s an example of how to use the isEmpty() method:

String str1 = "";
String str2 = "Hello, World!";

boolean isEmpty1 = str1.isEmpty(); // true
boolean isEmpty2 = str2.isEmpty(); // false

System.out.println(isEmpty1);
System.out.println(isEmpty2);

In the example above, str1 is an empty string, so isEmpty1 is true. str2 is not empty, so isEmpty2 is false.

Returns of Java String isEmpty():

The isEmpty() method in Java is a non-static method of the java.lang.String class that checks whether a string is empty or not. It returns a boolean value indicating whether the string is empty (true) or not (false).

If the string is empty (contains no characters), the isEmpty() method returns true. Otherwise, if the string contains at least one character, the isEmpty() method returns false.

Here’s an example of how to use the isEmpty() method:

String str1 = "";
String str2 = "Hello, World!";

boolean isEmpty1 = str1.isEmpty(); // true
boolean isEmpty2 = str2.isEmpty(); // false

System.out.println(isEmpty1);
System.out.println(isEmpty2);

In the example above, str1 is an empty string, so isEmpty1 is true. str2 is not empty, so isEmpty2 is false.

Internal implementation:

The internal implementation of the isEmpty() method in Java can vary depending on the version of the Java Virtual Machine (JVM) and the specific implementation of the java.lang.String class.

In general, the isEmpty() method checks whether the length of the string is equal to zero. If the length of the string is zero, then the string is empty and the method returns true. Otherwise, if the length of the string is greater than zero, then the string is not empty and the method returns false.

Here’s a possible implementation of the isEmpty() method:

public boolean isEmpty() {
    return length() == 0;
}

In the above implementation, the length() method returns the length of the string, and the isEmpty() method returns true if the length is zero, or false otherwise.

It’s worth noting that the length() method of the java.lang.String class is a native method, which means that its implementation is provided by the underlying platform (e.g., the JVM). Therefore, the actual implementation of the isEmpty() method can depend on the platform-specific implementation of the length() method.

Java String isEmpty() method example:

Here’s an example of how to use the isEmpty() method in Java:

public class StringExample {
    public static void main(String[] args) {
        String str1 = "";
        String str2 = "Hello, World!";
        
        if (str1.isEmpty()) {
            System.out.println("str1 is empty");
        } else {
            System.out.println("str1 is not empty");
        }
        
        if (str2.isEmpty()) {
            System.out.println("str2 is empty");
        } else {
            System.out.println("str2 is not empty");
        }
    }
}

In the above example, we create two string objects, str1 and str2. str1 is an empty string, and str2 contains the text “Hello, World!”.

We then use the isEmpty() method to check whether each string is empty or not. If a string is empty, we print the message “string is empty”. Otherwise, we print the message “string is not empty”.

When we run the above example, we get the following output:

str1 is empty
str2 is not empty

This output indicates that str1 is empty (contains no characters), while str2 is not empty (contains the text “Hello, World!”).

Empty Vs. Null Strings:

In Java, an empty string and a null string are two different things.

An empty string is a string that contains no characters, and it is represented by a string literal with zero length (i.e., "").

A null string, on the other hand, is a reference to an object that has not been initialized. A null string does not refer to a string object at all, and any attempt to call a method on a null string will result in a NullPointerException.

Here’s an example that demonstrates the difference between an empty string and a null string:

public class StringExample {
    public static void main(String[] args) {
        String str1 = ""; // empty string
        String str2 = null; // null string
        
        System.out.println(str1.isEmpty()); // true
        // System.out.println(str2.isEmpty()); // NullPointerException
        
        if (str1 == null) {
            System.out.println("str1 is null");
        } else if (str1.isEmpty()) {
            System.out.println("str1 is empty");
        } else {
            System.out.println("str1 is not empty or null");
        }
        
        if (str2 == null) {
            System.out.println("str2 is null");
        } else if (str2.isEmpty()) {
            System.out.println("str2 is empty");
        } else {
            System.out.println("str2 is not empty or null");
        }
    }
}

In the above example, we create two string objects, str1 and str2. str1 is an empty string, and str2 is a null string.

We first use the isEmpty() method to check whether str1 is empty or not. Since str1 is an empty string, the method returns true.

We then try to call the isEmpty() method on str2, which is a null string. This results in a NullPointerException at runtime, because we are trying to call a method on a null object.

Finally, we use an if statement to check whether each string is null, empty, or not empty. The output of this example is:

true
str1 is empty
str2 is null

Blank Strings:

In Java, a blank string is a string that contains only whitespace characters. Whitespace characters include spaces, tabs, and line breaks.

The isBlank() method was introduced in Java 11 to check whether a string is blank or not. This method returns true if the string is empty or contains only whitespace characters, and false otherwise.

Here’s an example that demonstrates the use of the isBlank() method:

public class StringExample {
    public static void main(String[] args) {
        String str1 = "";
        String str2 = "   ";
        String str3 = "Hello, World!";
        
        if (str1.isBlank()) {
            System.out.println("str1 is blank");
        } else {
            System.out.println("str1 is not blank");
        }
        
        if (str2.isBlank()) {
            System.out.println("str2 is blank");
        } else {
            System.out.println("str2 is not blank");
        }
        
        if (str3.isBlank()) {
            System.out.println("str3 is blank");
        } else {
            System.out.println("str3 is not blank");
        }
    }
}

In the above example, we create three string objects, str1, str2, and str3. str1 is an empty string, str2 contains only whitespace characters, and str3 contains the text “Hello, World!”.

We use the isBlank() method to check whether each string is blank or not. If a string is blank, we print the message “string is blank”. Otherwise, we print the message “string is not blank”.

When we run the above example, we get the following output:

str1 is blank
str2 is blank
str3 is not blank