seegongsik
Programming

What is a reference in programming

While a program runs, the values we create have to live somewhere. That place is memory. Picture a long wall of numbered lockers, with each name tag being a note that points to one of those lockers.

01

Values live somewhere

The whole time a program runs, every value takes up a spot somewhere in memory.
Memory is like a wall lined with numbered lockers.
Create a value and it slips into an empty locker, and the program remembers it by locker number.

slot 1
slot 2
slot 3
slot 4
slot 5

Press "store a value" and a value goes into an empty slot, with its slot number shown.

Each value now has its own slot and number.
But in code, we don't write "slot 7"; we use names like 'a' or "score."
So how is a name connected to a slot number?

02

A name tag points to a spot

A name isn't the value itself; it's a note that knows which slot to look in.
Name 'a' → slot 3 → the 7 inside it.
So when you use 'a', the program follows the note, opens slot 3, and reads 7. That's what "pointing" means.

a
→
slot 3
7

Press "follow a" and it follows the note to the slot and takes out the value inside.

The name points to a slot, and the value is inside that slot.
This pointing note is called a reference.
From here on, things get really important. What happens if you "give a to b as well"?

03

Small values get copied

Give a small value, like a number, to b, and that value is duplicated into a new slot.
Now a is in slot 3 and b is in slot 5: two different slots.
So even if you change a to 9, b stays 7. The two are independent, each with its own value.

a
→
slot 3
7

Press "copy to b," then "change a," and b doesn't change because it's in a different slot.

Copied small values don't affect each other, which keeps things tidy.
But big things like objects are a different story, because duplicating a big thing in full is heavy.
So something other than copying usually happens.

04

Big things point to the same spot

When you give an object to b, the whole object usually isn't duplicated; instead, b is made to point to the same slot.
Now a and b, two notes, point to one slot together.
So if you change the object through a, it looks changed through b too. One thing, two names.

a
↓
slot 4
object: 1

Press "b = a," then "change via a," and since it's the same slot, b shows the change too.

Two names point to one slot, so changing one changes both.
This is where beginners are most often surprised: "Why did b change when I didn't even touch it?"
Now you know the answer: they were pointing to the same slot.

05

Know what points where, and the confusion ends

The confusion always comes down to one thing.
Does the name have its own copy, or does it point to one thing together with another name?
A small value is copied, so the two move separately; a big thing is pointed to from the same spot, so they move together.
Keep this distinction in mind, and there are no more surprises.

Pick "small value" or "big thing" and have it do the same task, and the results split.

That's the foundation of programming.
Starting from a single value, we bundled, repeated, named, gathered things into objects, fixed them by debugging, and had a machine run them.
And now we've seen where all of it lives and what names point to, so you have a solid foundation.

In one lineA value lives in one slot (address) in memory, and a name is a note pointing to that slot. Give a small value to another name and it's copied into a new slot, so the two are independent; a big thing (an object) usually has both names pointing to the same slot, so changing one changes the other. Know what points where, and that confusion clears up.
← Programming
Was this helpful? Support seegongsik