Value and Reference Types
From SwinBrain
Object oriented programming languages often have two different sets of types, called value and reference types. Understanding the differences between these types is important in order to fully understand what occurs when working with the different types within these languages.
Value Types
A variable of a value type always contains a value of that type. The assignment to a variable of a value type creates a copy of the assigned value. Local variables of a value type will store their value on the stack, ensuring that the memory used is freed once the current method terminates.
The following pseudocode is used to illustrate how value types work.
Begin Main()
Declare x as an integer
Set x to 10
Declare y as a character
Set y to 'a'
Call M(y)
End
Begin M(z as character)
Declare a as character
Set a to z
End
The image shown here illustrates the creation of three variables x, y, and z. All of these variables are value types. This image show the state of the memory used by the program represented by the pseudocode once it reaches line 11. Notice that each variable has its own value, and that all values are stored on the Stack.
If we execute lines 12 and 13 then the picture changes a little as seen in the image to the left. A new variable named
a has been created and the
value from
z has been copied into it. Notice that if you changed the value of
a (e.g. Set a to "b") then this would have no impact on the values of either the
y variable in
Main, or the
z variable in
M. Each of the value type variables has its own storage space that contains the value that it represents.
Also See:
Reference Types
Unlike value types, reference types do not contain a value when first defined. In fact, the value of a reference type variable is not really the value but a reference to the value (hence the name reference type). You can think of a reference type variable as being a reference, or pointer, to the value. The following code illustrates some of the features of reference types. In this example we assume that the object type is a reference type.
Begin Main()
Declare x as an object
Set x to a new object
Call M(x)
End
Begin M(z as object)
Declare a as object
Set a to z
End
Memory after line 2 has executed.
The image to the left shows the state of the memory for the program once line 2 has been executed. You can see that the
x variable has been created on the stack and that its value is a reference that is currently referring to the null value indicating that the reference does not currently refer to any object.
Memory after line 3 has executed.
Line 3 of the pseudocode above has several effects on the memory used by the program. A new object (
An Object in the image) is created on the heap, and the
x variable is set to reference this new object. Notice that
x now has a value, but that the value is a reference to an object.
Memory after line 8 has executed.
When
M is executed on line 5, the value of
x is passed to the parameter
z. Because
x is a reference type, it is the reference that is copied not the value. The result of this is shown in the image to the left. You can see that
x and
z now both refer to the same object. Remember to think of reference variables as references to objects, when your copy these you get a copy of the reference.
Memory after line 10 has executed.
Line 10 copies the value of
z to the variable
a. As you can see in the image, this results in a copy of the reference. Now
x,
z, and
a all refer to the same object. If you update this object, changing any of its values, these effects can be seen via the other variables.
Further Information
<taglist>[programming, types, HIT3695]
Outlines the differences between value and reference types
</taglist>