Francisco
Pax
Info

Value and reference types, visualized

One of the proverbial things to understand in programming is the mouthful of why when we let a = 1, and we let b = a, and then we change a = 5, why b is still 1. In an attempt to clarify this, I created a one-sheet visualization of the matter at hand.

In essence, primitive values (i.e. things like strings, numbers, and booleans) are stored by storing the value. This means that the actual value is stored inside the variable. So when I tell the computer to store a in b, Iā€™m not storing a link from b to a, but a copy of the value originally stored in a.

More complex values (i.e. things like arrays or objects, or in lay terms, collections of primitive values) are stored by storing the reference to the value. This means that what gets stored in the variable is a reference to the location in memory where the data is stored.