Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Introduction

In the world of Java, the null type is pervasive, and it's hard to use the language without encountering it. In most cases, the intuitive understanding that it represents nothingness or lack of something suffices to program effectively. Nevertheless, sometimes we want to dig deeper and thoroughly understand the topic.

In this tutorial, we'll look at how the null type works under the hood and how it relates to other types.

2. What Is a Type?

Before we answer the specific question about the null type, we need to define what a type really is. This isn't an easy task because there are a lot of competing definitions. The one that will be the most useful to us is the definition of value space. In that definition, a type is defined by the set of possible values it can hold.

Let's say we want to declare a boolean variable:

boolean valid;

What we've done is declare that the variable named “valid” will hold one of two possible values: true or false. The set of possible values has only two elements. If we want to declare an int variable, the set of possible values would be much larger but still clearly defined: every possible number from -2^31 to 2^31-1.

3. What Is null‘s Type?

null is a special type that has only one possible value. In other words, the set of possible values has only one element. This characteristic alone makes the null type very peculiar. Normally, the whole purpose of variables is that they can assume different values. There's only one null reference, so a variable of the null type could only hold that one specific reference. It would bring no information apart from that the variable exists.

There's one trait that makes the null type usable in the way we use it. The null reference can be cast to any other reference type. That means we can treat it like a special literal, which can be of any non-primitive type. In practice, the null reference extends the effective set of possible values of these types.

That explains why we can assign the exact same null reference to variables of totally different reference types:

Integer age = null;
List<String> names = null;

That also explains why we can't assign the null value to variables of primitive types like boolean:

Boolean validReference = null // this works fine
boolean validPrimitive = null // this does not

It's because the null reference can be cast to a reference type but not to a primitive one. The set of possible values of a boolean variable will always have two elements.

4. null as a Method Parameter

Let's take a look at two simple methods, both taking one parameter but of different types:

void printMe(Integer number) {
  System.out.println(number);
}

void printMe(String string) {
  System.out.println(string);
}

Because of polymorphism in Java, we can call these methods like this:

printMe(6);
printMe("Hello");

The compiler will understand what method we're referencing. But the following statement will cause a compiler error:

printMe(null); // does not compile

Why? Because null can be cast to both String and Integer – the compiler won't know which method to choose.

5. NullPointerException

As we've seen already, we can assign the null reference to a variable of a reference type even though null is technically a different, separate type. If we try to use some property of that variable as if it wasn't null, we'll get a runtime exception – NullPointerException. It happens because the null reference isn't the type we're referencing it to be and doesn't have the properties we expect it to have:

String name = null;
name.toLowerCase(); // will cause exception at runtime

Before Java 14, NullPointerExceptions were short, simply stating in which line of the code the error happened. If the line was complex and had a chain of invocations, that information wasn't informative. However, from Java 14, we can rely on so-called Helpful NullPointerExceptions.

6. Conclusion

In this article, we looked closely at how the null type works. First, we defined a type, and then we found how the null type fits into that definition. Finally, we learned about how a null reference can be cast to any other reference type, making it the tool that we know and use.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments