1. Introduction
1.绪论
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.
在Java的世界里,null类型无处不在,不遇到它就很难使用这种语言。在大多数情况下,直观地理解它代表虚无或缺乏某种东西,就足以有效地进行编程。尽管如此,有时我们还是想深入挖掘,彻底了解这个话题。
In this tutorial, we’ll look at how the null type works under the hood and how it relates to other types.
在本教程中,我们将看看null类型是如何在引擎盖下工作的,以及它与其他类型的关系。
2. What Is a Type?
2.什么是类型?
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.
在我们回答关于null类型的具体问题之前,我们需要定义类型到底是什么。这并不是一件容易的事,因为有很多相互竞争的定义。对我们最有用的一个定义是价值空间的定义。在这个定义中,一个类型是由它所能容纳的可能的值的集合所定义的。
Let’s say we want to declare a boolean variable:
假设我们想声明一个boolean变量。
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 “的变量将持有两个可能的值之一。true 或 false。可能值的集合只有两个元素。如果我们想声明一个int变量,可能的值集会大得多,但仍有明确的定义:从-2^31到2^31-1的每个可能的数字。
3. What Is null‘s Type?
3.什么是null的类型?
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.
null是一个特殊的类型,只有一个可能的值。换句话说,可能值的集合只有一个元素。仅仅这一特性就使null类型非常特别。通常情况下,变量的全部目的是它们可以承担不同的值。只有一个null引用,所以一个null类型的变量只能容纳这一个特定的引用。除了变量的存在,它不会带来任何信息。
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.
有一个特性使得null类型可以以我们使用它的方式使用。null引用可以被转换为任何其他引用类型。这意味着我们可以把它当作一个特殊的字面,它可以是任何非原始类型。在实践中,null引用扩展了这些类型的可能值的有效集合。
That explains why we can assign the exact same null reference to variables of totally different reference types:
这就解释了为什么我们可以将完全相同的null引用分配给完全不同引用类型的变量。
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:
这也解释了为什么我们不能将null值分配给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.
这是因为null引用可以被投到一个引用类型,但不能投到一个原始类型。一个boolean变量的可能值集合总是有两个元素。
4. null as a Method Parameter
4.null作为一个方法参数
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:
由于Java中的多态性,我们可以像这样调用这些方法。
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.
为什么?因为null可以被转换为String和Integer – 编译器将不知道该选择哪种方法。
5. NullPointerException
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:
正如我们已经看到的,我们可以将null引用分配给一个引用类型的变量,尽管null在技术上是一个不同的独立类型。如果我们试图使用该变量的某些属性,就像它不是null一样,我们将得到一个运行时异常–NullPointerException。它的发生是因为null引用并不是我们要引用的类型,也不具备我们期望它具备的属性。
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
6.结语
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.
在这篇文章中,我们仔细研究了null类型是如何工作的。首先,我们定义了一个类型,然后我们发现null类型是如何融入这个定义的。最后,我们了解了null引用是如何被投射到任何其他引用类型的,从而使它成为我们所知道和使用的工具。