Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Nullable type

From Wikipedia, the free encyclopedia
Feature of some programming languages
Not to be confused withnullable symbol.
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding 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 someprogramming languages which allow a value to be set to the special value NULL instead of the usual possible values of thedata type. In statically typed languages, a nullable type is anoption type,[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 inNULL inSQL. In other words, NULL is undefined.

Primitive types such asintegers andBooleans cannot generally be null, but the corresponding nullable types (nullable integer and nullable Boolean, respectively) can also assume the NULL value.[jargon][citation needed] This can be represented in ternary logic as FALSE, NULL, TRUE as inthree-valued logic.

Example

[edit]

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 likeC# 2.0, a nullable integer, for example, can be declared by a question mark (int? x).[1][2]: 46  In programming languages likeC# 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 ofT? (or explicitlySystem.Nullable<T>) inC#.

usingSystem;publicclassExample{staticvoidMain(string[]args){// Using shorthandint?a=10;double?b=null;// Using explicit Nullable<T>Nullable<int>c=20;Nullable<double>d=5.5;// Checking if the nullable variables have valuesConsole.WriteLine(a.HasValue);// TrueConsole.WriteLine(b.HasValue);// FalseConsole.WriteLine(c.HasValue);// TrueConsole.WriteLine(d.HasValue);// True// Accessing the valuesif(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

[edit]

In contrast, objectpointers can be set toNULL 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 byC. A. R. Hoare in 1965 as part of theAlgol 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 has classes that correspond to scalar values, such as Integer, Boolean, and Float. Combined withautoboxing (automatic usage-driven conversion between object and value), this effectively allows nullable variables for scalar values.[citation needed]

Compared with option types

[edit]

Nullable type implementations usually adhere to thenull object pattern.

There is a more general and formal concept that extend the nullable type concept: it comes fromoption types, which enforce explicit handling of the exceptional case.

Language support

[edit]

The following programming languages support nullable types.

Statically typed languages with native null support include:

Statically typed languages with library null support include:

Dynamically-typed languages with null include:

  • JavaScript has anull andundefined values.
  • Julia has thenothing value (which is of typeNothing) and theUnion{T, Nothing} type idiom.[11]
  • Perl scalar variables default toundef and can be set toundef.
  • PHP with NULL type and is_null() method, native nullable type in version 7.1[12]
  • Python has theNone value.[13]
  • Ruby has the nil value and NilClass type.

See also

[edit]

References

[edit]
  1. ^"Nullable Types (C#)". Msdn.microsoft.com. Retrieved2013-08-19.
  2. ^abSkeet, Jon (23 March 2019).C# in Depth. Manning.ISBN 978-1617294532.
  3. ^"(luKa) Developer Zone - NullableTypes". Nullabletypes.sourceforge.net. Retrieved2013-08-19.
  4. ^"NullableTypes". Nullabletypes.sourceforge.net. Retrieved2013-08-19.
  5. ^Tony Hoare (2009)."Null References: The Billion Dollar Mistake". QCon London.
  6. ^"Optional Type - Ballerina Programming Language".
  7. ^BillWagner."Nullable value types - C# reference".docs.microsoft.com. Retrieved2021-10-05.
  8. ^"Sound null safety | Dart".
  9. ^"Null Safety - Kotlin Programming Language".
  10. ^KathleenDollard."Nullable Value Types - Visual Basic".docs.microsoft.com. Retrieved2021-10-05.
  11. ^"Types · the Julia Language".
  12. ^"PHP: RFC:nullable_types".
  13. ^"Built-in Constants — Python 3.9.5 documentation".
Nulls incomputing
Retrieved from "https://en.wikipedia.org/w/index.php?title=Nullable_type&oldid=1317902830"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp