Value type and reference type

From Wikipedia, the free encyclopedia
(Redirected from Reference type)

In computer programming, data types can be divided into two categories: value types (or by-value types) and reference types (or by-reference types). Value types are completely represented by their meaning, while reference types are references to another value.[1][2]

Value types[edit]

A value is a fully self-describing piece of data. Equal values are indistinguishable at program runtime. That is, they lack identity.

For example, in the C++ and Java programming languages, the integer literal 5 is a value. It is indistinguishable from any other 5 at runtime, and there is no way to mutate the literal 5 to be a different value such as 6.

In many programming languages, value types are often represented and stored in an efficient manner. For example, booleans, fixed-size integers, and fixed-size floating-point types may be compactly stored and passed in the registers of the CPU.

Reference types[edit]

Reference types are represented as a reference to another value, which may itself be either a value or reference type. Reference types are often implemented using pointers, though many high-level programming languages such as Java and Python do not expose these pointers to the programmer.

Reference types have identity, meaning that it is possible to distinguish two references at runtime, even when they contain underlying values that are equal. For example, consider the following class in Java:

public class BoxedInt {
    public int innerValue;

    public BoxedInt(int i) {
        this.innerValue = i;
    }
}

Two invocations of new BoxedInt(5) will yield two objects observably distinct from each other, even though they contain the same value 5. In this case, two separate memory allocations were made, and the value 5 placed into each. Changing the innerValue variable of one of the BoxedInts will not affect the innerValue of the other.

Classification per language[edit]

Language Value type Reference type
C++[3][4] all object types,α including booleans, characters, integer numbers, floating-point numbers, classes (including strings, lists, maps, sets, stacks, queues), enumerations, pointersβ pointers, references (both lvalue and rvalue)
Java[5] booleans, characters, integer numbers, floating-point numbers arrays, classes (including immutable strings, lists, dictionaries, sets, stacks, queues, enumerations), interfaces, null pointer
C#[6] structures (including booleans, characters, integer numbers, floating-point numbers, point in time i.e. DateTime, optionals i.e. Nullable<T>), enumerations classes (including immutable strings, arrays, tuples, lists, dictionaries, sets, stacks, queues), interfaces, pointers
Swift[7][8] 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[9] 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[10] immutable booleans, immutable floating-point numbers, immutable integer numbers (bigint), immutable strings, immutable symbols, undefined, null objects (including functions, arrays, typed arrays, sets, maps, weak sets and weak maps)
OCaml[11][12] 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 strings arrays, immutable strings, byte strings, dictionaries (including pointers)

Boxing and unboxing[edit]

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[edit]

Notes[edit]

The C++ standard does not use the term "value type" as defined above. Rather, a C++ object is defined as storing a value, which may be of a primitive or compound type; in addition, whether an object has a name or not is optional.[13] In the standard library, the term "value type" is largely restricted to type definitions inside container and wrapper class templates, where the specified type of the objects stored in an instance of the class template is given the moniker value_type[14] (for example, the types std::vector<T>::value_type and T are equivalent, where T is the type used to instantiate the vector).[15]
In C++, pointers are objects, unlike references: they store their own values, may or may not be named, and can be used as value types of container and wrapper class templates.[4][16]

References[edit]

  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. ^ a b "std::is_object - cppreference.com". en.cppreference.com.
  5. ^ "Chapter 4. Types, Values, and Variables". docs.oracle.com.
  6. ^ "C# Keywords". docs.microsoft.com.
  7. ^ "Structures and Classes — The Swift Programming Language (Swift 5.2)". docs.swift.org.
  8. ^ "Closures — The Swift Programming Language (Swift 5.2)". docs.swift.org.
  9. ^ "Built-in Types — Python 3.8.2rc1 documentation". docs.python.org.
  10. ^ "ECMAScript® 2019 Language Specification". www.ecma-international.org.
  11. ^ "Chapter 24 The core library". caml.inria.fr.
  12. ^ "Modifiable Data Structures". caml.inria.fr.
  13. ^ "Object - cppreference.com". en.cppreference.com.
  14. ^ "C++ named requirements: Container - cppreference.com". en.cppreference.com.
  15. ^ "std::vector - cppreference.com". en.cppreference.com.
  16. ^ "Pointer declaration - cppreference.com". en.cppreference.com.