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]
Even among languages that have this distinction, the exact properties of value and reference types vary from language to language, but typical properties include:
int
(a value type) into an Integer
object (an object type), or reversing this via "unboxing".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.
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.
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:
classFoo{publicinta;}Fooa=newFoo();Foob=a;a.prop=3;a=newFoo();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.
structFoo{inta;};Fooa;a.prop=1;Foo&b=a;Fooc=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).
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.
Language | Value type | Reference type |
---|---|---|
Java [4] | all non-object types, including (e.g.) booleans and numbers | 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 |
OCaml [10] [11] | 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 [12] |
In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.
In programming languages, name binding is the association of entities with identifiers. An identifier bound to an object is said to reference that object. Machine languages have no built-in notion of identifiers, but name-object bindings as a service and notation for the programmer is implemented by programming languages. Binding is intimately connected with scoping, as scope determines which names bind to which objects – at which locations in the program code (lexically) and in which one of the possible execution paths (temporally).
In object-oriented (OO) and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.
In computer programming, a reference is a value that enables a program to indirectly access a particular datum, such as a variable's value or a record, in the computer's memory or in some other storage device. The reference is said to refer to the datum, and accessing the datum is called dereferencing the reference. A reference is distinct from the datum itself.
In computer programming, a parameter or a formal argument is a special kind of variable used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are the values of the arguments with which the subroutine is going to be called/invoked. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call are evaluated, and the resulting values can be assigned to the corresponding parameters.
In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full. Later languages allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function.
In computer science, a union is a value that may have any of multiple representations or formats within the same area of memory; that consists of a variable that may hold such a data structure. Some programming languages support a union type for such a data type. In other words, a union type specifies the permitted types that may be stored in its instances, e.g., float
and integer
. In contrast with a record, which could be defined to contain both a float and an integer; a union would hold only one at a time.
In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.
In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The definition of a reference in C++ is such that it does not need to exist. It can be implemented as a new name for an existing object.
In some programming languages, const is a type qualifier that indicates that the data is read-only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in that it is part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time for each use. Languages which use it include C, C++, D, JavaScript, Julia, and Rust.
In a programming language, an evaluation strategy is a set of rules for evaluating expressions. The term is often used to refer to the more specific notion of a parameter-passing strategy that defines the kind of value that is passed to the function for each parameter and whether to evaluate the parameters of a function call, and if so in what order. The notion of reduction strategy is distinct, although some authors conflate the two terms and the definition of each term is not widely agreed upon.
The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.
this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity which the currently running code is a part of. The entity referred to thus depends on the execution context. Different programming languages use these keywords in slightly different ways. In languages where a keyword like "this" is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, these keywords can disambiguate variables and functions with the same name.
The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted. The Python language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages. It supports multiple programming paradigms, including structured, object-oriented programming, and functional programming, and boasts a dynamic type system and automatic memory management.
In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.
This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.
In computer programming, a constant is a value that is not altered by the program during normal execution. When associated with an identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution. To simplify, constants' values remains, while the values of variables varies, hence both their names.
C# 4.0 is a version of the C# programming language that was released on April 11, 2010. Microsoft released the 4.0 runtime and development environment Visual Studio 2010. The major focus of C# 4.0 is interoperability with partially or fully dynamically typed languages and frameworks, such as the Dynamic Language Runtime and COM.
Rust is a general-purpose programming language emphasizing performance, type safety, and concurrency. It enforces memory safety, meaning that all references point to valid memory, without a garbage collector. To simultaneously enforce memory safety and prevent data races, its "borrow checker" tracks the object lifetime of all references in a program during compiling.
Swift is a high-level general-purpose, multi-paradigm, compiled programming language created by Chris Lattner in 2010 for Apple Inc. and maintained by the open-source community. Swift compiles to machine code and uses an LLVM-based compiler. Swift was first released in June 2014 and the Swift toolchain has shipped in Xcode since version 6, released in 2014.