yohhoyの日記

技術的メモをしていきたい日記

nullの型

Java言語におけるnullリテラルは、言語仕様上は null type をもつ唯一の値として扱われる。null type は名前のない特別な型であるため、Javaプログラム上で null type を直接扱うことはできない。

Java Language Specification, Java SE 8 Editionより一部引用。

There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.
Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.
The null reference is the only possible value of an expression of null type.
The null reference can always be assigned or cast to any reference type (§5.2, §5.3, §5.5).
In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

Chapter 4. Types, Values, and Variables, 4.1. The Kinds of Types and Values

The type of a literal is determined as follows:

  • (snip)
  • The type of the null literal null (§3.10.7) is the null type (§4.1); its value is the null reference.
Chapter 15. Expressions, 15.8.1. Lexical Literals

関連URL