Nullable type

Nullable type

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.

Contents

Example

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 conception in the 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)[1]. In programming languages like C# 1.0 Nullable Types can be defined by an external library[2] as new types (e.g. NullableInteger, NullableBoolean)[3].

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".[4] 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[5], 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[6]

Language support

The following programming languages support nullable types:

  • Java
  • Oxygene
  • .NET Framework version 1.0 languages with the external NullableTypes library[2]
    This implementation adheres to the Null Object pattern, adheres to the NULL (SQL) Three-valued logic (3VL), includes all the .NET built-in types, requires programmers to implement the nullable behavior for a custom value-type.
  • .NET Framework version 2.0 languages
    This implementation violates the Null Object pattern, violates the NULL (SQL) Three-valued logic (3VL), makes use of the .NET 2.0 Generics so it includes all the .NET built-in types and can easily extends a custom value-type
  • Perl scalar variables default to undef and can be set to undef. Similarly, list and hash variables can be () or {} respectively.

See also

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Type system — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

  • Option type — For families of option contracts in finance, see Option style. In programming languages (especially functional programming languages) and type theory, an option type or maybe type is a polymorphic type that represents encapsulation of an optional …   Wikipedia

  • Oxygene (programming language) — Oxygene Developer RemObjects Software Stable release 3.0.21 (August 29, 2009; 2 years ago (2009 08 29)) Influenced by Object Pas …   Wikipedia

  • Pointer (computing) — This article is about the programming data type. For the input interface (for example a computer mouse), see Pointing device. Pointer a pointing to the memory address associated with variable b. Note that in this particular diagram, the computing …   Wikipedia

  • Null (SQL) — The Greek lowercase omega (ω) character is used to represent Null in database theory. Null is a special marker used in Structured Query Language (SQL) to indicate that a data value does not exist in the database. Introduced by the creator of the… …   Wikipedia

  • Null Object pattern — Null object redirects here. For the concept in category theory, see Initial object. In object oriented computer programming, a Null Object is an object with defined neutral ( null ) behavior. The Null Object design pattern describes the uses of… …   Wikipedia

  • Null function — Not to be confused with identity function or empty function. In computer science, a null function (or null operator) is subroutine that returns no data values and leaves the program state unchanged. When it is part of the instruction set of… …   Wikipedia

  • Null — may refer to: Contents 1 In computing 2 In art 3 In mathematics 4 In science 5 People …   Wikipedia

  • Null character — For other uses, see Null symbol. The null character (also null terminator), abbreviated NUL, is a control character with the value zero.[1] [2] It is present in many character sets, including ISO/IEC 646 (or ASCII), the C0 control code, the… …   Wikipedia

  • Empty string — In computer science and formal language theory, the empty string (or null string)[1] is the unique string of length zero. It is denoted with λ or sometimes Λ or ε. The empty string is distinct from a null reference in that in an object oriented… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”