To understand what null means in Java, let's look at an analogy with numbers: the number 0 symbolizes the absence of something, and null means the same thing when it comes to reference data types. If a field of a reference type (such as String, Object, or StringBuilder) is not explicitly assigned a value, then, by analogy with primitive types, it receives a default value, and that value is null:

Code Console output
public class Solution {

    public static int i;
    public static String s;

    public static void main(String[] args) {

But if you declare an array like this:

String[] strings = new String[12];

an array will be created containing 12 elements, and all of them will be null:

Code Console output
public class Solution {
    public static void main(String[] args) {
        String[] strings = new String[12];

        for (int i = 0; i < strings.length; i++) {
            System.out.println("Element " + i + ":" + strings[i]);
Element 0: null
Element 1: null
Element 2: null
Element 3: null
Element 4: null
Element 5: null
Element 6: null
Element 7: null
Element 8: null
Element 9: null
Element 10: null
Element 11: null

As you can see, when concatenated with a string, the value null becomes the string "null". That said, if you call the toString() method on it, like this:

String[] strings = null;

then you will get a NullPointerException (we'll discuss exceptions in detail later). The same thing happens if you try to call any other method on null (the exception is static methods, which you'll get to know shortly):

public static void main(String[] args) {
    StringBuilder sb = null;
    sb.append("test"); // This will compile, but there will be a runtime error

null is, among other things, a reserved keyword (like public or static), so you cannot create a variable, method, or class named null. Like the others, this keyword is case-sensitive (you may have noticed that we write null in lowercase everywhere). That means:

String firstName = Null; // Compilation error
String secondName = NULL; // Compilation error
String fullName = null; // This will compile

Let's look at what else you can and can't do with null:

  • You can assign null to any reference:

    StringBuilder sb = null;
  • null can be cast to any reference type:

    String s = (String) null; // This will compile, but doing this doesn't make any sense :)
  • null cannot be assigned to a primitive variable:

    int i = null; // This won't compile
  • null can be compared using == and !=

  • null == null returns true

In previous lessons, we talked about how everything in Java is an object, and every object has a type.

What can we say about null in this respect? null is a literal of a certain type, and this type has no name. And since this type has no name, it is impossible to declare a variable of this type, or to cast to it. Thus, null is the only representative of this unnamed type. In practice, we can ignore this type, and think of null as a special literal that can be assigned to any reference variable.

Things to remember:

  • null is the default value for reference data types
  • null means "no value"
  • If we call any method on an object whose value is null, the code will compile but at runtime we'll get a NullPointerException.