Explain Codes LogoExplain Codes Logo

Difference between null and empty ("") Java String

java
null-safety
string-manipulation
java-best-practices
Anton ShumikhinbyAnton Shumikhin·Nov 15, 2024
TLDR

In Java, a null string (String nullString = null;) signifies lack of memory allocation, which leads to a NullPointerException upon attempted operations such as nullString.length(). On the other hand, an empty string (String emptyString = "";) is a legitimate object without characters, thus emptyString.length() safely returns 0. The direct comparison (==) discriminates null as absence of an object and empty as presence of an object holding zero characters.

String nullString = null; String emptyString = ""; // Null check boolean isNull = (nullString == null); // true, null has left the chat boolean isEmpty = (emptyString == null); // false, string may be empty but it's still here partying! // Length operation int emptyLength = emptyString.length(); // 0, it's an existent party with no guests! int nullLength = nullString.length(); // Throws NullPointerException, party location never existed!

Key distinctions

It's crucial to remember the following when working with strings in Java:

  • The null reference symbolizes the non-existence of an object.
  • An empty string is an object without characters (length() == 0).
  • Literal strings including the empty string are interned in Java.
  • null has no properties or methods to carry out direct method invocation.
  • Evaluating a null string against an empty string employing == or equals() will always give false.

Coding scenarios

Exploring string interning and equality

String s1 = ""; String s2 = ""; boolean literalEquality = (s1 == s2); // true, empty strings party in the same memory spot!

Probing string operations

String s = ""; // These operations are a go for empty strings char firstChar = s.charAt(0); // Throws StringIndexOutOfBoundsException, because the party is empty! boolean isEmpty = s.isEmpty(); // true, there are no characters partying on the string! String n = null; // These operations on null will lead to NullPointerException char firstChar = n.charAt(0); // Throws NullPointerException, no party to attend to begin with! boolean isNull = n.isEmpty(); // Throws NullPointerException, null can't host a party!

Introducing null-safe operations

// Leveraging Apache Commons Lang boolean isBlank = StringUtils.isBlank(null); // true, it's not just a party pooper, but a party non-exister boolean isNotBlank = StringUtils.isNotBlank(""); // false, the party venue is open but no one showed up // Employing Java 11 isBlank() boolean isBlankNative = "".isBlank(); // true, super boring party with no attendees

Deeper understanding of null vs empty

Grasping the differentiation prevents unpredictable behaviors and coding errors. For instance, an application expecting a string may display different behavior if provided a null value instead of an empty string, leading to null-related exceptions.

Design with null and empty strings

Accurate discernment between a null and an empty string can steer design choices. While null might hint at the absence of a value, using an empty string ("") indicates that the value was purposely set to be empty.

Strengthening your code

Cope with null and empty strings

  • Use conditional checks: Always verify if the string is null before initiating operations to ward off NullPointerException.
  • Enlist Optional: Java 8 introduced Optional to handle potential null cases.