||This article needs additional citations for verification. (March 2009)|
In programming, nullable types are a feature of some statically-typed programming languages which allows a data type to be set to the special value NULL instead of their common range of possible values. For value types or built-in data types like integers and booleans however, such behavior is mostly not possible. Nullable type support allows for the programmer to make also these value types NULL. This can be useful in general and also when working with databases. A field in a Relational database like SQL may have an entry that is NULL (or empty) instead of containing a value. A language with nullable type support can then return a NULL value and correctly represent the behavior of the database.
An integer variable may contain a positive or negative natural number ( ..., -2, -1, 0, 1, 2, ...). The value 0 (zero) however is not the correct representation for this variable to contain nothing (it contains the value 0 and only the convention in a given context might suggest that 0 represents "nothing" instead of the actual representation: "nothing" of "something"). In many circumstances it is required to represent also the fact that a variable has not been given any value at all. This 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 even 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 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 language. Hoare later described their 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. In some languages, like Objective-C, however, NULL object pointers can be used without problems.
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. Option type implementations usually adhere to the Special Case pattern.
Language support 
The following programming languages support nullable types:
- PHP with NULL type and is_null() method
- The Microsoft .NET Framework languages.
- Perl scalar variables default to
undefand can be set to
See also