TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

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.

Equals
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(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.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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf