Null and Empty String in Java

Mohammad Irfan Oct 12, 2023
  1. Use the Empty String in Java
  2. Use the Null String in Java
  3. Null and empty String Example in Java
  4. Check Null and empty String using the equals() method in Java
Null and Empty String in Java

This tutorial will discuss the difference between null and empty strings in Java. But before that, we will understand the basic difference between the empty and null terms.

Empty is like an empty box which we can use as per our need to fill it or to do needful.

Null is like a vacuum with some property associated with it, due to which we can neither consider it to be empty nor full.

In Java, a string refers to the sequence of characters. For example, delftstack is a string.

We often see the empty and null strings in Java. Many people think that both the empty and null strings are the same, but there is a difference between null and empty strings.

Use the Empty String in Java

String a = ""; // empty string

Here a is an empty string. When we assign the empty string to the string variable, it indicates that the reference variable refers to a memory location of a string in a heap.

An empty string is a string that has no characters in it, and it has a well-defined length - the length is 0. We can perform all the string operations on an empty string.

We can find its length by using the length() method, finding out the index of some characters, etc.

Use the Null String in Java

String b = null;

Here b is a null string. When assigning the null to the string variable, the reference variable does not refer to any memory location in a heap.

The null string means no string at all. It does not have a length because it’s not a string at all.

Applying any standard string operation to the null string will cause a NullPointerException runtime.

Null and empty String Example in Java

In this example, we created an empty and a null String, and then we checked their working with the length() method. The null string throws an exception while the empty does not.

See the example below.

public class SimpleTesting {
  public static void main(String[] args) {
    // empty string
    String a = "";
    // null string
    String b = null;

    // printing length of empty string
    System.out.println("length a = " + a.length());

    // this piece of code will still throw nullpointerexception .*
    if (b != "") {
      // printing length of null string
      System.out.println("length b =" + b.length());
    }
  }
}

Output:

length a = 0
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "<local2>" is null
        at SimpleTesting.main(SimpleTesting.java:15)

Check Null and empty String using the equals() method in Java

We used the equals() method and equal == operator to check the empty and null string in this example. The expression a==b will return false because "" and null do not occupy the same space in memory.

In simple words, we can say that variables don’t point to the same objects. The a.equals(b) will return false because the object reference values pointed by a and b do not match.

The b.equal(a) will return NullPointerExpception because b points to an obscure reference, and no operation is allowed.

public class SimpleTesting {
  public static void main(String[] args) {
    // empty string
    String a = "";
    // null string
    String b = null;
    System.out.println(a == b);
    System.out.println(a.equals(b));
    System.out.println(b.equals(a));
  }
}

Output:

false
false
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because "<local2>" is null
        at SimpleTesting.main(SimpleTesting.java:13)

Related Article - Java String