Reference type

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In programming language theory, a reference type is a data type that refers to an object in memory. A pointer type on the other hand refers to a memory address. Reference types can be thought of as pointers that are implicitly dereferenced.[1] The objects being referred to are dynamically allocated on the heap whereas value types are allocated automatically on the stack. In languages supporting garbage collection the objects being referred to are destroyed automatically after they become unreachable.

When a reference type variable refers to an immutable object it behaves with the same semantics as a primitive value type. The fact that the object being referred to cannot be modified by any of the references to it means the only way to change the value of the reference variable is through assignment. The use of the Number classes in the Java programming language is an example of this behavior.[2]

Language and platform support[edit]

  • In C++, booleans, characters, integer numbers, floating-point numbers, arrays, classes—including strings, lists, dictionaries, sets, stacks, queues—and enumerations are value types, while references and pointers are reference types.[3]
  • In C#, structures (struct keyword)—including booleans, characters, integer numbers, floating-point numbers and fixed-point numbers, lists, dictionaries, sets, stacks, queues, optionals—and enumerations are value types, while classes (class keyword)—including immutable strings, arrays, tuples, lists, dictionaries, sets, stacks, queues—, interfaces and pointers are reference types.[4]
  • In Swift, structures (struct keyword)—including booleans, characters, integer numbers, floating-point numbers, fixed-point numbers, immutable strings, tuples, lists, dictionaries, sets, stacks, queues—and enumerations—including optionals—are value types, while classes (class keyword) and interfaces are reference types.[5]
  • In Java, booleans, characters, integer numbers and floating-point numbers, are value types, while arrays, classes—including immutable strings, lists, dictionaries, sets, stacks, queues, enumerations—and interfaces are reference types.[6]
  • In Python, 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—are reference types.[7]
  • In JavaScript, immutable booleans, immutable floating-point numbers, immutable symbols, immutable strings, undefined, prototypes—including lists—are reference types.[8]
  • In OCaml, immutable characters, immutable integer numbers, mutable floating-point numbers, immutable tuples, immutable enumerations —including immutable units, immutable booleans, immutable lists, immutable optionals—, immutable exceptions are immutable formatting strings are value types, while arrays, immutable strings, byte strings and dictionaries—including references—are value types.[9],[10]

References[edit]

See also[edit]