Java – Check if a String is Blank
A string is said to be empty if the string contains no characters in it. In other words, a string is empty if its length is zero.
To check if a String str
is empty, use String.isEmpty() method. Call isEmpty() method on the string str
. If str
is empty, then isEmpty() method returns true.
Java Program
</>
Copy
public class Example {
public static void main(String[] args) {
String str = "";
boolean result = str.isBlank();
System.out.println("Is our string empty? " + result);
}
}
Output
Is our string empty? true
If str
length is not zero, then isEmpty() method returns false.
Java Program
</>
Copy
public class Example {
public static void main(String[] args) {
String str = "hello";
boolean result = str.isBlank();
System.out.println("Is our string empty? " + result);
}
}
Output
Is our string empty? false
Conclusion
In this Java Tutorial, we learned how to check if a string is empty in Java.