Nullable types are a feature of some
programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the
data type. In statically typed languages, a nullable type is an
option type, while in dynamically typed languages (where values have types, but variables do not), equivalent behavior is provided by having a single null value.
NULL is frequently used to represent a missing value or invalid value, such as from a function that failed to return or a missing field in a database, as in
NULL
Null may refer to:
Science, technology, and mathematics Astronomy
*Nuller, an optical tool using interferometry to block certain sources of light Computing
*Null (SQL) (or NULL), a special marker and keyword in SQL indicating that a data value do ...
in
SQL. In other words, NULL is undefined.
Primitive types such as
integer
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
s and
Booleans cannot generally be null, but the corresponding nullable types (nullable integer and nullable Boolean, respectively) can also assume the NULL value. This can be represented in ternary logic as FALSE, NULL, TRUE as in
three-valued logic.
Example
An integer variable may represent integers, but 0 (zero) is a special case because 0 in many programming languages can mean "false". Also, this does not provide any notion of saying that the variable is empty, a need that arises in many circumstances. This need can be achieved with a nullable type. In programming languages like
C# 2.0, a nullable integer, for example, can be declared by a question mark (int? x).
In programming languages like
C# 1.0, nullable types can be defined by an external library
as new types (e.g. NullableInteger, NullableBoolean).
A Boolean variable makes the effect more clear. Its values can be either "true" or "false", while a nullable Boolean may also contain a representation for "undecided". However, the interpretation or treatment of a logical operation involving such a variable depends on the language.
Compared with null pointers
In contrast, object
pointers can be set to
NULL
Null may refer to:
Science, technology, and mathematics Astronomy
*Nuller, an optical tool using interferometry to block certain sources of light Computing
*Null (SQL) (or NULL), a special marker and keyword in SQL indicating that a data value do ...
by default in most common languages, meaning that the pointer or reference points to nowhere, that no object is assigned (the variable does not point to any object). Nullable references were invented by
C. A. R. Hoare in 1965 as part of the
Algol W
ALGOL W is a programming language. It is based on a proposal for ALGOL X by Niklaus Wirth and Tony Hoare as a successor to ALGOL 60. ALGOL W is a relatively simple upgrade of the original ALGOL 60, adding string, bitstring, complex number a ...
language. Hoare later described his invention as a "billion-dollar mistake". This is because object pointers that can be NULL require the user to check the pointer before using it and require specific code to handle the case when the object pointer is NULL.
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
has classes that correspond to scalar values, such as Integer, Boolean, and Float. Combined with
autoboxing (automatic usage-driven conversion between object and value), this effectively allows nullable variables for scalar values.
Compared with option types
Nullable type implementations usually adhere to the
null object pattern.
There is a more general and formal concept that extend the nullable type concept: it comes from
option types, which enforce explicit handling of the exceptional case.
Language support
The following programming languages support nullable types.
Statically typed languages with native null support include:
*
Ballerina
*
C#
*
Dart
*
Kotlin
*
Ceylon
*
SQL
*
SAS (Missing values)
Statically typed languages with library null support include:
*
C# (since version 2)
*
Delphi
Delphi (; ), in legend previously called Pytho (Πυθώ), was an ancient sacred precinct and the seat of Pythia, the major oracle who was consulted about important decisions throughout the ancient Classical antiquity, classical world. The A ...
*
Free Pascal
*
VB.NET
*
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
(since version 8)
*
Scala
*
Oxygene
*
F#
* Statically typed
CLI languages
Dynamically-typed languages with null include:
*
JavaScript has a
null
and
undefined
values.
*
Julia has the
nothing
value (which is of type
Nothing
) and the
Union
type idiom.
*
Perl scalar variables default to
undef
and can be set to
undef
.
*
PHP with NULL type and is_null() method, native nullable type in version 7.1
*
Python has the
None
value.
*
Ruby has the nil value and NilClass type.
See also
*
Null coalescing operator
*
Semipredicate problem
*
Union type
*
Unit type
References
{{nulls
Type theory