Value type and reference type

Short description: Classes of data types

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]

## Contents

* 1 Properties of value types and reference types
* 2 Reference types and "call by sharing"
* 3 Reference types vs. explicit pointers
* 4 Reference rebinding and aliasing
* 5 Immutable data types
* 6 Classification per language
* 7 See also
* 8 References

## 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:

* Primitive data types, such as Booleans, fixed-size integers, floating-point values, and characters, are value types.
* Objects, in the sense of object-oriented programming, belong to reference types.
* Assigning to a variable of reference type simply copies the reference, whereas assigning to a variable of value type copies the value. This applies to all kinds of variables, including local variables, fields of objects, and array elements. Likewise when calling a function: parameters of reference type are copies of the reference, whereas parameters of value type are copies of the value.
* If a reference type is mutable, then mutations made via one reference are visible via any other, whereas if a value type is immutable, then mutations made to one value are not visible in another.
* Reference types support the notion of identity -- it makes sense to discuss whether two values of reference type refer to the same object, and the language provides functionality to determine whether they do -- whereas value types do not.
* Values of reference type refer to objects allocated in the heap, whereas values of value type are contained either on the call stack (in the case of local variables and function parameters) or inside their containing entities (in the case of fields of objects and array elements). (With reference types, it is only the reference itself that is contained either on the call stack or inside a containing entity.)
* Reference types support the notion of subtyping, whereby all values of a given reference type are automatically values of a different reference type. Value types do not support subtyping, but may support other forms of implicit type conversion, e.g. automatically converting an integer to a floating-point number if needed. Additionally, there may be implicit conversions between certain value and reference types, e.g. "boxing" a primitive

int

|

(a value type) into an

Integer

|

object (an object type), or reversing this via "unboxing".

## 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 is 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.

## Reference rebinding and aliasing

In most programming languages, it is possible to change the variable of a reference type to refer to another object, i.e. to rebind the variable to another object.

For example, in the following Java code:

class Foo {
public int a;

Foo a = new Foo();
Foo b = a;
a.prop = 3;
a = new Foo();
a.prop = 1;

`Foo` is a reference type, where `a` is initially assigned a reference of a new object, and `b` is assigned to the same object reference, i.e. bound to the same object as `a`, therefore, changes through `a` is also visible to `b` as well. Afterwards, `a` is assigned a reference (rebound) to another new object, and now `a` and `b` refer to different objects. At the end, `a` refers to the second object with its `prop` field having the value `1`, while `b` refers to the first object with its `prop` field having the value `3`.

However, such as C++, the term "reference type" is used to mean an alias, and it is not possible to rebind a variable of a reference type once it is created, as it is an alias to the original object.

class Foo {
public:
int a;

Foo a;
a.prop = 1;
Foo& b = a;
Foo c = a;
a.prop = 3;

In C++, all non-reference class types have value semantics. In the above example, `b` is declared to be a reference (alias) of `a`, and for all purposes, `a` and `b` are the same thing. It is impossible to rebind `b` to become something else. After the above example is run, `a` and `b` are the same `Foo` object with `prop` being `3`, while `c` is a copy of the original `a` with `prop` being `1`.

In C#, apart from the distinction between value types and reference types, there is also a separate concept called reference variables.[3] A reference variable, once declared and bound, behaves as an alias of the original variable, but it can also be rebounded to another variable by using the reference assignment operator `= ref`. The variable itself can be of any type, including value types and reference types, i.e. by passing a variable of a reference type by reference (alias) to a function, the object where the reference-type variable points to can also be changed, in addition to the object itself (if it is mutable).

## Immutable data types

If an object is immutable and object equality is tested on content rather than identity, the distinction between value type and reference types is no longer clear, because the object itself cannot be modified, but only replaced as a whole (for value type) / with the reference pointed to another object (for reference type). Passing such immutable objects between variables have no observable differences if the object is copied or passed by reference, unless the object identity is taken. In a functional programming language where nothing is mutable (such as Haskell), such distinction does not exist at all and becomes an implementation detail.

## Classification per language

Language | Value type | Reference type

|
|

Java[4] | all non-object types, including (e.g.) Booleans and numbers (support for object value types proposed for Project Valhalla) | all object types, including (e.g.) arrays
C++ | all data types, except reference types, array types and function types | arrays and functions
C#[5] | all non-object types, including structures and enumerations as well as primitive types | all object-types, including both classes and interfaces
Swift[6][7] | structures (including e.g. Booleans, numbers, strings, and sets) and enumerations (including e.g. optionals) | functions, closures, classes
Python[8] |  | all types
JavaScript[9] | all non-objects, including Booleans, floating-point numbers, and strings, among others | all objects, including functions and arrays, among others
Rust[10] | All types that implement the Copy trait | All types that do not implement the Copy trait
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
PHP | Non-object types, such as primitives and arrays. | All object (class) types [13]

## See also

* Primitive data type
* Composite data type

## References

1. ↑ Brown, Erik E. (2006). Windows Forms in Action. Shelter Island, New York: Manning. p. 703. ISBN 978-1-932-39465-8. https://books.google.com/books?id=PrZQAAAAMAAJ&q="value+type"+"reference+type".
2. ↑ Stephens, Rod (2014). C# 5.0 Programmer's Reference. Indianapolis, Indiana: John Wiley & Sons. p. 57. ISBN 978-1-118-84728-2. https://books.google.com/books?id=DEM_AwAAQBAJ&q="value+type"+"reference+type"&pg=PA57.
3. ↑ "Reference variables". https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/declarations#reference-variables.
4. ↑ "Chapter 4. Types, Values, and Variables". https://docs.oracle.com/javase/specs/jls/se14/html/jls-4.html.
5. ↑ "C# Keywords". https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/.
6. ↑ "Structures and Classes — The Swift Programming Language (Swift 5.2)". https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html.
7. ↑ "Closures — The Swift Programming Language (Swift 5.2)". https://docs.swift.org/swift-book/LanguageGuide/Closures.html.
8. ↑ "Built-in Types — Python 3.8.2rc1 documentation". https://docs.python.org/3/library/stdtypes.html.
9. ↑ "ECMAScript® 2019 Language Specification". https://www.ecma-international.org/ecma-262/#sec-ecmascript-language-types.
10. ↑ "Trait Copy in std::marker — Rust". https://doc.rust-lang.org/std/marker/trait.Copy.html.
11. ↑ "Chapter 24 The core library". https://caml.inria.fr/pub/docs/manual-ocaml/core.html.
12. ↑ "Modifiable Data Structures". https://caml.inria.fr/pub/docs/oreilly-book/html/book-ora026.html.
13. ↑ "PHP: The Basics". https://www.php.net/manual/en/language.oop5.basic.php.

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Value type and reference type. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Value_type_and_reference_type&oldid=4226899"