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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Learn next
- Garbage collectionThe runtime traces what a program can still reach and frees everything else. Intermediate
- Stack vs heap memoryFrames that come and go with each call, and blocks you request and must give back. Intermediate
- Pure functionsFunctions that only turn inputs into an output, and why that makes code easy to trust. Beginner
Spotted a mistake on this page?