Reference Types
   HOME

TheInfoList



OR:

In computer programming,
data type In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
s 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 Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a ''name'' ...
to another value.


Value types

A value is a fully self-describing piece of data. Equal values are indistinguishable at during program runtime. That is, they lack identity. For example, in the C++ and Java programming languages, the integer literal is a value. It is indistinguishable from any other at runtime, and there is no way to mutate the literal to be a different value such as . 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

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 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 Two invocations of will yield two objects observably distinct from each other, even though they contain the same value . In this case, two separate memory allocations were made, and the value placed into each. Changing the variable of one of the s will not affect the of the other.


Classification per language


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

* Primitive data type * Composite data type


References

{{DEFAULTSORT:Value Type Data types