{{Short description|Feature of some programming languages}} {{Distinguish|Nullable symbol}} {{More citations needed|date=March 2009}}
'''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,{{citation needed|date=October 2020}} 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 in SQL. In other words, NULL is undefined.
Primitive types such as integers and Booleans cannot generally be null, but the corresponding nullable types (nullable integer and nullable Boolean, respectively) can also assume the NULL value.{{Technical statement|date=July 2019}}{{citation needed|date=October 2020}} 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).<ref name="msdn">{{cite web|url=http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx |title=Nullable Types (C#) |publisher=Msdn.microsoft.com |date= |accessdate=2013-08-19}}</ref><ref name=Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}</ref>{{rp|46}} In programming languages like C# 1.0, nullable types can be defined by an external library<ref name="sourceforge">{{cite web|url=https://nullabletypes.sourceforge.net/ |title=(luKa) Developer Zone - NullableTypes |publisher=Nullabletypes.sourceforge.net |date= |accessdate=2013-08-19}}</ref> as new types (e.g. NullableInteger, NullableBoolean).<ref name="onlinedoc">{{cite web|url=https://nullabletypes.sourceforge.net/onlinedoc-v1.2/ |title=NullableTypes |publisher=Nullabletypes.sourceforge.net |date= |accessdate=2013-08-19}}</ref>
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.
The following is an example of <code>T?</code> (or explicitly <code>System.Nullable<T></code>) in C#. <syntaxhighlight lang="csharp"> using System;
public class Example { static void Main(string[] args) { // Using shorthand int? a = 10; double? b = null;
// Using explicit Nullable<T> Nullable<int> c = 20; Nullable<double> d = 5.5;
// Checking if the nullable variables have values Console.WriteLine(a.HasValue); // True Console.WriteLine(b.HasValue); // False Console.WriteLine(c.HasValue); // True Console.WriteLine(d.HasValue); // True
// Accessing the values if (a.HasValue) { Console.WriteLine(a.Value); // 10 }
if (b.HasValue) { Console.WriteLine(b.Value); // (won't be executed because b is null) } } }
</syntaxhighlight>
==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 his invention as a "billion-dollar mistake".<ref>{{cite web |url=http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare |title=Null References: The Billion Dollar Mistake |location=QCon London |year=2009 |author=Tony Hoare}}</ref> 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. <!-- Not really true about Objective-C: In some languages, like Objective-C,<ref name="Objective-C messaging">{{cite web|url=http://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html#//apple_ref/doc/uid/TP40011210-CH4-SW22 |title=Programming with Objective-C: Working with Objects |publisher=Developer.apple.com |date=2012-12-13 |accessdate=2013-08-19}}</ref> however, NULL object pointers can be used without problems. -->
Java 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.{{Citation needed|date=July 2019}}
==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 <ref>{{cite web |url=https://ballerina.io/learn/by-example/optional-type.html |title=Optional Type - Ballerina Programming Language }}</ref> *C#<ref>{{Cite web|last=BillWagner|title=Nullable value types - C# reference|url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types|access-date=2021-10-05|website=docs.microsoft.com|language=en-us}}</ref> * Dart<ref>{{cite web |title=<nowiki>Sound null safety | Dart</nowiki> |url=https://dart.dev/null-safety }}</ref> * Kotlin <ref>{{cite web |url=https://kotlinlang.org/docs/reference/null-safety.html |title=Null Safety - Kotlin Programming Language }}</ref> * Ceylon * SQL * SAS (Missing values)
Statically typed languages with library null support include: * C# (since version 2)<ref name=Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}</ref>{{rp|5}} * Delphi * Free Pascal * VB.NET<ref>{{Cite web|last=KathleenDollard|title=Nullable Value Types - Visual Basic|url=https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/nullable-value-types|access-date=2021-10-05|website=docs.microsoft.com|language=en-us}}</ref> * Java (since version 8) * Scala * Oxygene * F# * Statically typed CLI languages
Dynamically-typed languages with null include: * JavaScript has a <code>null</code> and <code>undefined</code> values. * Julia has the <code>nothing</code> value (which is of type <code>Nothing</code>) and the <code>Union{T, Nothing}</code> type idiom.<ref>{{Cite web|url=https://docs.julialang.org/en/v1/manual/types/#Type-Unions|title = Types · the Julia Language}}</ref> * Perl scalar variables default to <code>undef</code> and can be set to <code>undef</code>. * PHP with NULL type and is_null() method, native nullable type in version 7.1 <ref>{{Cite web|url=https://wiki.php.net/rfc/nullable_types|title=PHP: RFC:nullable_types}}</ref> * Python has the <code>None</code> value.<ref>{{Cite web|url=https://docs.python.org/3/library/constants.html#None|title = Built-in Constants — Python 3.9.5 documentation}}</ref> * Ruby has the nil value and NilClass type.
==See also== * Null coalescing operator * Semipredicate problem * Union type * Unit type
==References== {{Reflist}}
{{nulls}}
Category:Type theory