Home
Java
String isEmpty Method
Updated Jul 15, 2022
Dot Net Perls
Empty strings, isEmpty. Some strings exist and are not null, but have zero characters. We can test for this case with the isEmpty method.
With isEmpty, we detect when a string has a length of 0. We can also test the length() method for the value 0. And with String equals() we can compare an empty string.
String length
First example. This program uses the isEmpty method. We declare a string called "value" and assign it to an empty string literal.
Result The isEmpty method returns true. So the inner statements of the if-block are reached.
if
public class Program { public static void main(String[] args) { String value = ""; // See if string is empty. if (value.isEmpty()) { // Display string length. System.out.println("String is empty, length = " + value.length()); } } }
String is empty, length = 0
Equals, empty strings. We can compare a string to see if it is empty with equals(). We cannot use the equals operator == as this will compare the references identities.
And Two empty strings can be separate in memory. This means they would have difference identities.
However String equals() compares the inner characters of a string. This can correctly tell us if the string is empty.
String equals
public class Program { public static void main(String[] args) { String value = ""; // Two equal string literals can be tested for identity. if (value == "") { System.out.println("Test 1"); } // A new empty string will not have the same identity. // ... This test will not succeed. String empty = new String(""); if (value == empty) { System.out.println("Test 2"); // Not reached. } // The equals method tests for character equivalence. // ... This test will succeed. if (value.equals(empty)) { System.out.println("Test 3"); } } }
Test 1 Test 3
NullPointerException. We cannot use the isEmpty method on a null String reference. We must first test against null if the String reference might be null.
public class Program { public static void main(String[] args) { String value = null; // This program will not work. if (value.isEmpty()) { System.out.println(0); } } }
Exception in thread "main" java.lang.NullPointerException at Program.main(Program.java:6)
Performance. I tested the performance of string equals() and isEmpty. The Java runtime is well-optimized. In my simple benchmark, no measurable differences were present.
A summary. Empty strings are often present in Java programs. We use them to avoid null strings, which can cause NullPointerExceptions.
try
Testing empty strings. I feel isEmpty is the best option, but sometimes testing length() for values is appropriate. It is rare that using string equals() is needed here.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jul 15, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen