isEmpty() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The isEmpty() is an inbuilt method of the String class that is used to check whether is the given String is empty or not. If the string is empty the isEmpty() returns true, otherwise false boolean value is returned by the isEmpty() method.

The Syntax of isEmpty() method in java is given below:

Parameters of isEmpty() in Java

The isEmpty() method doesn't take any parameter.

Return Values of isEmpty() in Java

The isEmpty() method returns boolean value i.e true or false. The true is returned when the given String is empty.

Exception for of isEmpty() in Java

The isEmpty() method throws a NullPointerException when the string is initialized to null. Because Null is a placeholder that generally means "no data about this is available", and it is different from an empty string that's why the compiler cannot handle it and raise a NullPointerException.

We can handle this exception using the try and catch block. We write all the code in the body of the try block and use the catch block to handle the NullpointerException when it occurs in the try block.

Example

In the below program, we have two strings, one string is containing some value and the other is empty. Let's understand how to use isEmpty() method to check whether the string is empty or not.

Output:

Explanation

The First string i.e "Scaler" is not an empty string that's why false is returned, On the other hand, the second string ie. "" is empty that's why true is returned.

Java string isempty()

The String class of java has the isEmpty() method that is used to check whether the given string is empty or not. This method returns a boolean value i.e true or false.

Example 1: Exception in isEmpty() method.

Output:

Explanation

The Exception has been raised because we are using the isEmpty() method for a string that is containing a null value. The isEmpty() cannot be checked for null values.

Example 2: Java String isEmpty() In the below program, we have two strings, one string is containing string value and the other is empty. Let's understand how to use isEmpty() method to check whether the string is empty or not.

Output:

Explanation

The First string is not an empty string that's why false is returned, On the other hand, the second string is empty that's why true is returned.

Example 3: Check if the string is null or empty Let's understand how to check whether the string is null or empty using the isEmpty() method. The isEmpty() method raises an exception if the string is null. We can simply avoid the exception by comparing the string value.

Let's understand how to check if the string is null or empty.

In the below program, we are checking whether the string is null or empty.

Output:

Explanation

The String is empty is returned for the first case because str==null is true, and in the second case the condition str1.isEmpty() is true that's why the String is empty is returned.

Conclusion

  • The isEmpty() method is an inbuilt method of the String class.
  • The isEmpty() method cannot be used for a string that is containing a null value.