In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
,
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
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
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
Pointer may refer to:
Places
* Pointer, Kentucky
* Pointers, New Jersey
* Pointers Airport, Wasco County, Oregon, United States
* The Pointers, a pair of rocks off Antarctica
People with the name
* Pointer (surname), a surname (including a li ...
, 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
In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled ...
*
Composite data type
References
{{DEFAULTSORT:Value Type
Data types