Java String isEmpty Method (Null, Empty Strings)

Test for empty strings with the isEmpty method and length. Use String equals to compare empty strings.
Empty strings, isEmpty. A string exists. It is not null. But it has zero characters and is empty. We can test for this case with the isEmpty method.Strings
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.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.

Java program that uses isEmpty 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()); } } } Output 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.

Java program that uses equals, tests empty strings 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"); } } } Output 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.
Java program that causes NullPointerException public class Program { public static void main(String[] args) { String value = null; // This program will not work. if (value.isEmpty()) { System.out.println(0); } } } Output Exception in thread "main" java.lang.NullPointerException at Program.main(
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.NullPointerException
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.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls