Null Value

Strings

Null Value

🙋 Need help? Ask an expert now!

"Null" is a value that signifies "no object". If we declare a new String object and forgets to initialize it with a value, it will have a value of null. A String can also be initialized with a value of null as follows: String varOne = null;. Take a look at the example below.

NullValueExample.java
package exlcode;

public class NullValueExample {

  // exampleVariableOne is only declared and not initialised
  public static String exampleVariableOne;

  public static void main(String[] args) {
    System.out.println(exampleVariableOne);
  }
}

In Java, variables created from the String class are like containers, they hold a reference to an object. Therefore, creating 10 strings will create 10 objects and 10 different variables that refer to the different objects. If variables that hold objects exist, we need a way to describe those variables that are not holding or referencing anything - this is where the null value comes in. It tells us that nothing is being held or referenced by the String.

Edit Me on GitHub!

Application Question

Which of the following creates a String with the value null?