Java String isEmpty() Method with Examples

The “isEmpty” function is a method commonly used in programming languages to check whether a string is empty or not. It returns a boolean value, typically “true” if the string has no characters (i.e., it is empty) and “false” if the string contains one or more characters. This function is useful for validating user input, handling empty string cases, or performing conditional operations based on the presence or absence of characters in a string.

String isempty

IsEmpty() String system checks whether a String is empty or not. This system returns true if the given string is empty. Differently, it returns false. The isEmpty() system of the String class has been included in Java string since JDK1.6. In other words, you can say that this system returns true if the length of the string is 0.

Parameters

None.

Technical Details

Returns: A boolean value:

  • true – The string is empty (length() is 0)
  • false – The string is not empty

Signature

The signature or syntax of string isEmpty() method is given below:

public boolean isEmpty()

Returns

true if the length is 0; otherwise, false.
Since
1.6
Internal implementation

Output:
true
False

Java String isEmpty() Method

Example 2

public class IsEmptyExample2 {  
public static void main(String[] args) {  
        String s1="";    
        String s2="TechVidvan";             
        
        if(s1.length()==0 || s1.isEmpty())  
            System.out.println("String s1 is empty");  
        else System.out.println("s1");        
        if(s2.length()==0 || s2.isEmpty())  
            System.out.println("String s2 is empty");  
        else System.out.println(s2);  
    }  
}

Output:
String s1 is empty
TechVidvan

Example 3

public class StringIsEmptyExample3   
{  
// main method  
public static void main(String argvs[])  
{  
String str = null;  
if(str.isEmpty())  
{  
System.out.println("The string is null.");  
}  
else  
{  
System.out.println("The string is not null.");  
}  
}  
}

Output:

Exception in thread “main” java.lang.NullPointerException
at StringIsEmptyExample3.main(StringIsEmptyExample3.java:7)
Here, we can use the == operator to check for the null strings.

Example 4

class StringIsEmptyExample4  
{  
// main method  
public static void main(String argvs[])  
{  
String str = null;  
if(str == null)  
{  
System.out.println("The string is null.");  
}  
else  
{  
System.out.println("The string is not null.");  
}  
}  
}

Output:
The string is null.

Blank Strings

In Java, a blank string usually refers to an empty string, which is a string that contains no characters. You can create an empty string in Java in several ways:

Using double quotes with no characters inside:

java
String blankString = "";

Using the `String` class constructor with no arguments:

java
String blankString = new String();

Both of these methods will create a string variable `blankString` that contains no characters and is considered empty or blank.

public class StringIsEmptyExample5   
{  
// main method  
public static void main(String argvs[])  
{  
// a blank string  
String str = "     ";  
int size = str.length();  
// trim the white spaces and after that  
// if the string results in the empty string   
// then the string is blank; otherwise, not.  
if(size == 0)  
{  
System.out.println("The string is empty. \n");  
}  
else if(size > 0 && str.trim().isEmpty())  
{  
System.out.println("The string is blank. \n");  
}  
else  
{  
System.out.println("The string is not blank. \n");  
}   
str = " Welcome to TechVidvan.  ";  
size = str.length();  
if(size == 0)  
{  
System.out.println("The string is empty. \n");  
}  
if(size > 0 && str.trim().isEmpty())  
{  
System.out.println("The string is blank. \n");  
}  
else  
{  
System.out.println("The string is not blank. \n");  
}  
}  
}

Output:
The string is blank.
The string is not blank.

Conclusion

In conclusion, the isEmpty() method in Java is a powerful tool that allows developers to efficiently check if a string is empty or null, simplifying the process of handling strings in programming. By leveraging this method, developers can write cleaner and more readable code, enhancing the overall quality and performance of their Java applications.

Understanding how to use isEmpty() effectively can significantly improve the robustness of your programs, ensuring that they handle empty or null strings gracefully without encountering errors. As you continue your journey in Java programming, mastering this method will prove invaluable in creating reliable and user-friendly applications.