What Is the null Type in Java? – 什么是Java中的null类型?

最后修改: 2022年 9月 15日

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.

我们所做的是声明名为 “valid “的变量将持有两个可能的值之一。truefalse。可能值的集合只有两个元素。如果我们想声明一个int变量,可能的值集会大得多,但仍有明确的定义:从-2^31到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) {

void printMe(String string) {

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



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.

为什么?因为null可以被转换为StringInteger – 编译器将不知道该选择哪种方法。

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.

在Java 14之前,NullPointerExceptions很短,只是简单地说明了错误发生在代码的哪一行。如果这一行很复杂,而且有一连串的调用,那么这些信息就没有意义了。然而,从Java 14开始,我们可以依靠所谓的有用的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.