References

A reference is a handle that leads to an object. Seven steps show how two names can share one object, how that differs from a pointer, and the bug it causes.

Beginner7 steps~6 min
Variable — a named box that holds a reference
Reference — an arrow from a variable to an object
Object — the array itself, stored once
// THE SHORT VERSION

In 30 seconds

A reference is a way to reach an object that lives somewhere else in memory. A variable holding a reference does not contain the object; it leads to it.

Assigning one such variable to another copies the reference, so both names lead to the same object. The second name is an alias, and a change made through either name is visible through the other.

A pointer is a rawer tool: an address stored as a number that code can change freely. References in Java, Python and JavaScript hide the address, and the runtime keeps the object alive while a reference reaches it.

References, step by step

STEPS01 / 07

Addresses in step 4 are made up, with 4-byte integers. [...scores] is a shallow copy: the array is new, but objects stored inside it would still be shared.

STEP 01 / 07 A name and an object
THE MATH
let a = [1, 2, 3] a = reference to the array
  1. Step 1: A name and an object. Line 1 does two things. It builds an array object somewhere in memory, and it makes the variable a refer to it. The variable does not contain the array. It holds a reference: a handle that leads to the object, drawn here as an arrow.
  2. Step 2: A second name: an alias. let b = a copies the arrow, not the array. Now two names lead to one object. b is called an alias of a: a second name for something that already exists. No new array was made, however large the first one is.
  3. Step 3: Change through one name. b.push(4) follows b’s arrow and adds 4 to the array it finds there. Reading a follows the other arrow to that same array, so a now has four items even though the code never mentions a on that line.
  4. Step 4: Pointers are different. C and C++ also have pointers. A pointer is a variable that stores a memory address as a number: you can print it, add to it to step to the next item, aim it elsewhere, or set it to null. A C++ reference is stricter: it is bound to one object when created and is used exactly like that object.
  5. Step 5: Managed references. Java, C#, Python and JavaScript sit in between. A reference there can be aimed at another object or at nothing (null, or None in Python), like a pointer. But the address stays hidden and cannot be computed with. The runtime tracks every reference and keeps an object alive while any of them reaches it.
  6. Step 6: The aliasing bug. A common mistake: the programmer wants a sorted list and the original order. But ranked = scores made an alias, not a second array, so sorting ranked reorders scores as well. Nothing on the sort line mentions scores, which is what makes aliasing bugs hard to spot.
  7. Step 7: The fix: a real copy. To get an independent list you must ask for a copy. [...scores] builds a new array with the same items, and ranked refers to that one. Now sorting ranked leaves scores alone. Python’s list(scores) and Java’s new ArrayList<>(scores) do the same job.
VALUES SO FAR
aref to arrayvariable
b····alias
a····shared
p····pointer
r····alias
a····re-aimed
scores····bug
scores····fixed

Spotted a mistake on this page?