Language: EN

tipo-valor-y-referencia

Type value and reference

We have already seen what REFERENCES are, elements that contain a link to another element. If you haven’t seen it yet, it’s worth taking a look at this What is a reference.

Now let’s see what implications the REFERENCES have in the field of computer science and programming. In particular, in the management of our variables.

In fact, most languages divide their variables into two types:

  • Value variables
  • Reference variables

The concept of REFERENCE is very “core” to your computer. It plays a crucial role in memory management and its internal operation.

These two types, value and reference, have different behaviors within a program. Affecting issues such as speed, mutability, dynamic memory.

So it is important to understand it so that you don’t screw up. And for general culture, which is not bad either. So let’s get to the point 👇.

What are Value Types and Reference Types

Value Types

Value variables are those in which the data is stored directly in the variable. That is, your variable has a “little space” to store its value.

tipo-valor

In general, value types are limited to the simplest types of each language. In particular, they are usually only:

  • Integers, and floating point numbers
  • Characters (not strings)
  • Booleans
  • Aggregations of simple variables

This means that when we assign one variable to another, an independent copy of the data is made.

Reference Types

Reference variables are those in which the variables contain references to the data instead of the data itself. That is, your variable contains a link to some data, which is “elsewhere”.

In fact, the REFERENCE does have a value. What happens is that the value is the way to access the data (the address):

tipo-referencia

Reference types include:

  • Collections (arrays, lists, dictionaries…)
  • Objects
  • Complex data structures

In short, basically everything is a REFERENCE. Except numbers, characters… and a little bit more.

Differences between Value Types and Reference Types

The main difference between a value type and a reference type is how they behave when copied. Let’s see it by making an example.

We do the same experiment, with these steps:

  • We create a variable A and assign it a value
  • We create another variable B and equal it to A
  • We modify B
  • We see what has happened to A

That is, we want to know if modifying B also modifies A.

Value type

Let’s start with a value type. For example, an integer

int A = 10;  // create a variable A, and assign it the value 10
int B = A;   // create a variable B, and assign it the value of A

int B = 20;  // change B. Has A changed?

How much is A worth at the end of the process? In this case, A is worth 10 and B is worth 20.

With value type: Modifying B -> NO ❌ it has modified A

This is because A and B are value types, they are independent. Each has its own value.

tipo-valor-ejemplo

By copying the value of A into B, we have copied its value. But they are still separate variables. Any subsequent changes do not affect the other, because there is no link between them.

Reference type

Now let’s do the same with a reference type. For example, let’s take an array. We are only going to work with position 0 of the array, enough to show what we want to see.

So we do the same experiment:

int[] A = {10, 0, 0};  // create a variable A
int[] B = A;           // create a variable B, and assign it the value of A

B[0] = 20;             // change B. Has A changed?

How much is A[0] worth? In this case, A[0] is worth 20.

With reference type: Modifying B -> YES ✔️ it has modified A

This has happened because we are working with reference types. When creating variable A, it was a reference to an array. When creating the new variable B, we have equalized it to A. Therefore, we had two references, which pointed to the same Array.

tipo-referencia-ejemplo

Therefore, if we modify anything in B, we are modifying it in A. Because both variables point to the same data.

If we wanted to make them independent again, we would have to assign a different Array to B.

int[] A = {10, 0, 0};   // create a variable A
int[] B = A;            // A and B point to the same data


int[] B = {10, 0, 0};   // now B points to another Array
B[0] = 20;              // if we change B, it no longer affects A

tipo-referencia-ejemplo-2

Internal operation Advanced

The first thing you need to know is that memory is organized in cells. Each of these cells are identified with a number.

tipo-valor-referencia-interno-1

When we create a variable such as A or B, the program takes this as an alias for memory addresses. Internally, the computer neither sees your aliases A nor B. Instead, it translates it to, for example, 0x17 and 0x21 (or whatever it corresponds to). And it only works with that.

When working with value types variables, in these memory cells it will store the value of your variable. In our example, 10. However, they are two independent cells. If I put 20 in one, the other is not affected.

tipo-valor-referencia-interno-2

When you have reference types variables, you also have two cells for your variables. But, instead of having the value, this time they have the memory address where the data is “actually”.

In our example, let’s say we have an ARRAY. The computer has created this array in position 0x30. Our variables A and B, have this position as a value.

tipo-valor-referencia-interno-3

So, both A and B are modifying the same data, which is actually in memory position 0x30.

On the other hand, your programming language knows how to handle both types. When accessing:

  • value types: It knows that it has the value available at the address that it corresponds to
  • reference types: It knows that it is a memory address, and that it has to “jump” to the data

This operation of “jump” that it has to do with reference types to reach the actual data is called indirection.

In principle, it is the reason why accessing a reference type variable is a little slower than accessing value types. But it is so common and so optimized that the difference is usually minimal or even zero. But it is there.

On the contrary, reference types are much faster to copy or move. This is because you are not really copying the data, you are only copying the memory address. Which, in general, occupies a few bytes. While the actual data can be… as large as your computer’s memory allows.