
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - String isEmpty() Method
Description
The Java String isEmpty() method is used to check whether the current string is empty or not. The method returns a boolean value which is true if and only if the string is empty; false otherwise. The isEmpty() method does not accept any parameter.
Note: If the string is, initialized as null, the isEmpty() method throws a NullPointerException. Because Null is a placeholder that generally means, "no data is available about this", and it is not the same as an empty string, the compiler cannot handle it and throws a NullPointerException.
Syntax
Following is the syntax of the Java String isEmpty() method −
public boolean isEmpty()
Parameters
It does not accept any parameter.
Return Value
This method returns true if length() is 0, else false.
Checking if an Empty String is Empty Example
If the given string is empty, the isEmpty() method returns true.
In the following program, we are creating a string literal with an empty value. Then, using the isEmpty() method, we are, trying to check whether the current string is an empty string or not.
package com.tutorialspoint; public class CheckEmpty { public static void main(String[] args) { //create a string literal String str = ""; System.out.println("The given string is an empty." + str); System.out.println("Length of the string is: " + str.length()); //using the isEmpty() method System.out.println("The current string is an empty string or not? " + str.isEmpty()); } }
Output
On executing the above program, it will produce the following result −
The given string is an empty. Length of the string is: 0 The current string is an empty string or not? true
Checking if String is Empty Example
If the given string is non-empty, this method returns false.
In the following example, we are instantiating the string class with the value "TutorialsPoint". Using the isEmpty() method, we are trying to check whether this string is empty or not.
package com.tutorialspoint; public class CheckEmpty { public static void main(String[] args) { //instantiate the string class String str = new String("TutorialsPoint"); System.out.println("The given string is: " + str); System.out.println("Length of the string is: " + str.length()); //using the isEmpty() method System.out.println("The current string is an empty string or not? " + str.isEmpty()); } }
Output
Following is the output of the above program −
The given string is: TutorialsPoint Length of the string is: 14 The current string is an empty string or not? false
Checking if String is Empty Example
Using the conditional statement to check whether the current string is empty or not.
In this program, we create an object of the string class with the value "Hello". Then, using the isEmpty() method and conditional statement, we are trying to check whether the current string is empty or not.
package com.tutorialspoint; public class CheckEmpty { public static void main(String[] args) { //create an object of the string class String str = new String("Hello"); System.out.println("The given string is: " + str); System.out.println("Length of the string is: " + str.length()); //using the isEmpty() method boolean bool = str.isEmpty(); if(bool) { System.out.println("The string is an empty string."); } else { System.out.println("The string is not an empty string"); } } }
Output
The above program, produces the following output −
The given string is: Hello Length of the string is: 5 The string is not an empty string
Getting Null Pointer Exception While Checking if String is Empty Example
If the given string is null, this method throws the NullPointerException.
In the following program, we are creating a string literal with the null value. Using the isEmpty() method, we are trying to check whether the given string is empty or not. Since the given string is null, this method throws an exception.
public class CheckEmpty { public static void main(String[] args) { try { //create the string literal String str = null; System.out.println("The given string is: " + str); System.out.println("Length of the string is: " + str.length()); //using the isEmpty() method boolean bool = str.isEmpty(); if(bool) { System.out.println("The string is an empty string."); } else { System.out.println("The string is not an empty string"); } } catch(NullPointerException e) { System.out.println("Exception: " + e); } } }
Output
After executing the above program, it will produce the following output −
The given string is: null Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null