Value type and reference type

In computer programming, data types can be divided into two categories: value types and reference types. A value of value type is the actual value. A value of reference type is a reference to another value.[1][2]

Classification per language

LanguageValue typeReference type
C++[3]booleans, characters, integer numbers, floating-point numbers, classes (including strings, lists, maps, sets, stacks, queues), enumerationsreferences, pointers
Java[4]booleans, characters, integer numbers, floating-point numbersarrays, classes (including immutable strings, lists, dictionaries, sets, stacks, queues, enumerations), interfaces, null pointer
C#[5]structures (including booleans, characters, integer numbers, floating-point numbers, fixed-point numbers, lists, dictionaries, sets, stacks, queues, optionals), enumerationsclasses (including immutable strings, arrays, tuples, lists, dictionaries, sets, stacks, queues), interfaces, pointers
Swift[6][7]structures (including booleans, characters, integer numbers, floating-point numbers, fixed-point numbers, mutable strings, tuples, mutable arrays, mutable dictionaries, mutable sets), enumerations (including optionals), and user-defined structures and enumerations composing other value types.functions, closures, classes
Python[8]classes (including immutable booleans, immutable integer numbers, immutable floating-point numbers, immutable complex numbers, immutable strings, byte strings, immutable byte strings, immutable tuples, immutable ranges, immutable memory views, lists, dictionaries, sets, immutable sets, null pointer)
JavaScript[9]immutable booleans, immutable floating-point numbers, immutable symbols, immutable strings, undefined, prototypes (including lists, null pointer)
OCaml[10][11]immutable characters, immutable integer numbers, immutable floating-point numbers, immutable tuples, immutable enumerations (including immutable units, immutable booleans, immutable lists, immutable optionals), immutable exceptions, immutable formatting stringsarrays, immutable strings, byte strings, dictionaries (including pointers)

Boxing and unboxing

Programming languages that distinguish between value types and reference types typically offer a mechanism, called boxing, to wrap some or all of their value types in reference types. This permits the use of value types in contexts expecting reference types. The converse process (to unwrap the value type) is known as unboxing.

See also

References

  1. Brown, Erik E. (2006). Windows Forms in Action. Shelter Island, New York: Manning. p. 703. ISBN 978-1-932-39465-8.
  2. Stephens, Rod (2014). C# 5.0 Programmer's Reference. Indianapolis, Indiana: John Wiley & Sons. p. 57. ISBN 978-1-118-84728-2.
  3. "Type - cppreference.com". en.cppreference.com.
  4. "Chapter 4. Types, Values, and Variables". docs.oracle.com.
  5. "C# Keywords". docs.microsoft.com.
  6. "Structures and Classes — The Swift Programming Language (Swift 5.2)". docs.swift.org.
  7. "Closures — The Swift Programming Language (Swift 5.2)". docs.swift.org.
  8. "Built-in Types — Python 3.8.2rc1 documentation". docs.python.org.
  9. "ECMAScript® 2019 Language Specification". www.ecma-international.org.
  10. "Chapter 24 The core library". caml.inria.fr.
  11. "Modifiable Data Structures". caml.inria.fr.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.