stringtranslate.com

Value type and reference type

In certain computer programming languages, data types are classified as either value types or reference types, where reference types are always implicitly accessed via references, whereas value type variables directly contain the values themselves.[1][2]

Properties of value types and reference types

Even among languages that have this distinction, the exact properties of value and reference types vary from language to language, but typical properties include:

Reference types and "call by sharing"

Even when function arguments are passed using "call by value" semantics (which is always the case in Java, and is the case by default in C#), a value of a reference type is intrinsically a reference; so if a parameter belongs to a reference type, the resulting behavior bears some resemblance to "call by reference" semantics. This behavior is sometimes called call by sharing.

Call by sharing resembles call by reference in the case where a function mutates an object that it received as an argument: when that happens, the mutation will be visible to the caller as well, because the caller and the function have references to the same object. It differs from call by reference in the case where a function assigns its parameter to a different reference; when that happens, this assignment will not be visible to the caller, because the caller and the function have separate references, even though both references initially point to the same object.

Reference types vs. explicit pointers

Many languages have explicit pointers or references. Reference types differ from these in that the entities they refer to are always accessed via references; for example, whereas in C++ it's possible to have either a std::string and a std::string *, where the former is a mutable string and the latter is an explicit pointer to a mutable string (unless it's a null pointer), in Java it is only possible to have a StringBuilder, which is implicitly a reference to a mutable string (unless it's a null reference).

While C++'s approach is more flexible, use of non-references can lead to problems such as object slicing, at least when inheritance is used; in languages where objects belong to reference types, these problems are automatically avoided, at the cost of removing some options from the programmer.

Classification per language

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. ^ "Chapter 4. Types, Values, and Variables". docs.oracle.com.
  4. ^ "C# Keywords". docs.microsoft.com.
  5. ^ "Structures and Classes — The Swift Programming Language (Swift 5.2)". docs.swift.org.
  6. ^ "Closures — The Swift Programming Language (Swift 5.2)". docs.swift.org.
  7. ^ "Built-in Types — Python 3.8.2rc1 documentation". docs.python.org.
  8. ^ "ECMAScript® 2019 Language Specification". www.ecma-international.org.
  9. ^ "Chapter 24 The core library". caml.inria.fr.
  10. ^ "Modifiable Data Structures". caml.inria.fr.