# Nullable type

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Nullable_type
> Markdown URL: https://mediated.wiki/source/Nullable_type.md
> Source: https://en.wikipedia.org/wiki/Nullable_type
> Source revision: 1344232326
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Feature of some programming languages

Not to be confused with [Nullable symbol](/source/Nullable_symbol).

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Nullable type" – news · newspapers · books · scholar · JSTOR (March 2009) (Learn how and when to remove this message)

**Nullable types** are a feature of some [programming languages](/source/Programming_languages) which allow a value to be set to the special value NULL instead of the usual possible values of the [data type](/source/Data_type). In statically typed languages, a nullable type is an [option type](/source/Option_type),[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed)*] 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](/source/Null_(SQL)) in [SQL](/source/SQL). In other words, NULL is undefined.

[Primitive types](/source/Primitive_type) such as [integers](/source/Integer) and [Booleans](/source/Boolean_data_type) cannot generally be null, but the corresponding nullable types (nullable integer and nullable Boolean, respectively) can also assume the NULL value.[*[jargon](https://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style#Technical_language)*][*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed)*] This can be represented in ternary logic as FALSE, NULL, TRUE as in [three-valued logic](/source/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#](/source/C_Sharp_(programming_language)) 2.0, a nullable integer, for example, can be declared by a question mark (int? x).[1][2]: 46 In programming languages like [C#](/source/C_Sharp_(programming_language)) 1.0, nullable types can be defined by an external library[3] as new types (e.g. NullableInteger, NullableBoolean).[4]

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 T? (or explicitly System.Nullable<T>) in [C#](/source/C_Sharp_(programming_language)).

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)
        }
    }
}

## Compared with null pointers

In contrast, object [pointers](/source/Pointer_(computer_programming)) can be set to [NULL](/source/Null_pointer) 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](/source/C._A._R._Hoare) in 1965 as part of the [Algol W](/source/Algol_W) language. Hoare later described his invention as a "billion-dollar mistake".[5] 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](/source/Java_(programming_language)) has classes that correspond to scalar values, such as Integer, Boolean, and Float. Combined with [autoboxing](/source/Autoboxing) (automatic usage-driven conversion between object and value), this effectively allows nullable variables for scalar values.[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed)*]

## Compared with option types

Nullable type implementations usually adhere to the [null object pattern](/source/Null_object_pattern).

There is a more general and formal concept that extend the nullable type concept: it comes from [option types](/source/Option_type), 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](/source/Ballerina_(programming_language)) [6]

- [C#](/source/C_Sharp_(programming_language))[7]

- [Dart](/source/Dart_(programming_language))[8]

- [Kotlin](/source/Kotlin_(programming_language)) [9]

- [Ceylon](/source/Ceylon_(programming_language))

- [SQL](/source/SQL)

- [SAS](/source/SAS_(software)) (Missing values)

Statically typed languages with library null support include:

- [C#](/source/C_Sharp_(programming_language)) (since version 2)[2]: 5

- [Delphi](/source/Delphi_(software))

- [Free Pascal](/source/Free_Pascal)

- [VB.NET](/source/Visual_Basic_.NET)[10]

- [Java](/source/Java_(programming_language)) (since version 8)

- [Scala](/source/Scala_(programming_language))

- [Oxygene](/source/Oxygene_(programming_language))

- [F#](/source/F_Sharp_(programming_language))

- Statically typed [CLI languages](/source/List_of_CLI_languages)

Dynamically-typed languages with null include:

- [JavaScript](/source/JavaScript) has a null and undefined values.

- [Julia](/source/Julia_(programming_language)) has the nothing value (which is of type Nothing) and the Union{T, Nothing} type idiom.[11]

- [Perl](/source/Perl_(programming_language)) scalar variables default to undef and can be set to undef.

- [PHP](/source/PHP) with NULL type and is_null() method, native nullable type in version 7.1 [12]

- [Python](/source/Python_(programming_language)) has the None value.[13]

- [Ruby](/source/Ruby_(programming_language)) has the nil value and NilClass type.

## See also

- [Null coalescing operator](/source/Null_coalescing_operator)

- [Semipredicate problem](/source/Semipredicate_problem)

- [Union type](/source/Union_type)

- [Unit type](/source/Unit_type)

## References

1. **[^](#cite_ref-msdn_1-0)** ["Nullable Types (C#)"](http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx). Msdn.microsoft.com. Retrieved 2013-08-19.

1. ^ [***a***](#cite_ref-Skeet_2-0) [***b***](#cite_ref-Skeet_2-1) Skeet, Jon (23 March 2019). *C# in Depth*. Manning. [ISBN](/source/ISBN_(identifier)) [978-1617294532](https://en.wikipedia.org/wiki/Special:BookSources/978-1617294532).

1. **[^](#cite_ref-sourceforge_3-0)** ["(luKa) Developer Zone - NullableTypes"](https://nullabletypes.sourceforge.net/). Nullabletypes.sourceforge.net. Retrieved 2013-08-19.

1. **[^](#cite_ref-onlinedoc_4-0)** ["NullableTypes"](https://nullabletypes.sourceforge.net/onlinedoc-v1.2/). Nullabletypes.sourceforge.net. Retrieved 2013-08-19.

1. **[^](#cite_ref-5)** Tony Hoare (2009). ["Null References: The Billion Dollar Mistake"](http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare). QCon London.

1. **[^](#cite_ref-6)** ["Optional Type - Ballerina Programming Language"](https://ballerina.io/learn/by-example/optional-type.html).

1. **[^](#cite_ref-7)** BillWagner. ["Nullable value types - C# reference"](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types). *docs.microsoft.com*. Retrieved 2021-10-05.

1. **[^](#cite_ref-8)** ["Sound null safety | Dart"](https://dart.dev/null-safety).

1. **[^](#cite_ref-9)** ["Null Safety - Kotlin Programming Language"](https://kotlinlang.org/docs/reference/null-safety.html).

1. **[^](#cite_ref-10)** KathleenDollard. ["Nullable Value Types - Visual Basic"](https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/nullable-value-types). *docs.microsoft.com*. Retrieved 2021-10-05.

1. **[^](#cite_ref-11)** ["Types · the Julia Language"](https://docs.julialang.org/en/v1/manual/types/#Type-Unions).

1. **[^](#cite_ref-12)** ["PHP: RFC:nullable_types"](https://wiki.php.net/rfc/nullable_types).

1. **[^](#cite_ref-13)** ["Built-in Constants — Python 3.9.5 documentation"](https://docs.python.org/3/library/constants.html#None).

v t e Nulls in computing Null character Null device Null function Null modem Null object pattern Null pointer Null in SQL Null string Null coalescing operator See also: Null coalescing operator Nullable type Undefined value

---
Adapted from the Wikipedia article [Nullable type](https://en.wikipedia.org/wiki/Nullable_type) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Nullable_type?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
