The Plain Picture#
Imagine a street with a billion houses, one after another, each holding one small box. How do you tell a delivery person which box you want? By house number.
The Real Name#
RAM is a long row of bytes, and each byte has a number: its memory address. Addresses start at 0
and go up. They're usually written in hex (Chapter 3), like 0x7ffe4a2c.
When a program stores our score (200) in memory, it's really saying "put this byte pattern at address such-and-such". When it reads the score, it asks for the bytes at that address.
A value that holds the address of something else is called a pointer (in C) or a reference (in Java and Python). It's like writing "the box you want is at house 5,042" on a note instead of carrying the box itself.
Under the Hood#
Storing a number that needs several bytes#
A Java int is 4 bytes (Chapter 4.1). Storing our score 200 at address 0x1000:
address: 0x1000 0x1001 0x1002 0x1003
byte: C8 00 00 00 (200 = 0x000000C8)Notice the smallest byte (C8) came first. Most CPUs today (x86-64 and ARM in normal mode) store
multi-byte numbers smallest-byte-first. This is called little-endian. The opposite order is
big-endian, and it is the standard order for data sent over networks ("network byte order"). You
rarely deal with this by hand, but it's why binary file formats and network protocols must state their
byte order.
Why "64-bit" matters#
A 64-bit computer uses 64-bit numbers for addresses. With 32-bit addresses you can only number 2Β³Β² bytes = 4 GB β which is why old 32-bit computers couldn't use more than 4 GB of RAM. 64-bit addresses can, in theory, number 16 billion GB, which is far more than any machine has.
A reference in action#
Variable "note" (a reference) The actual text, somewhere else in RAM
ββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
β 0x5000 β ββββ points βββΆ β 0x5000: 48 69 20 F0 9F 98 80β
ββββββββββββββββββββββ β H i _ π β
ββββββββββββββββββββββββββββββββThe variable holds just a small address. The 7 bytes of Hi π (Chapter 4.3) live elsewhere.
You'll see in Classes, Objects and Memory that this is exactly how Java objects work,
and why changing an object through one variable can "surprisingly" change it for another.
Gotchas
Two variables can refer to the same thing. If both hold address
0x5000, changing the text through one changes what the other sees. This causes many real bugs.Programs don't see real RAM addresses directly β the operating system gives each program its own private "fake" address space. That's virtual memory, taught in the next file (Operating Systems).
Check yourself
4 questionsWhat is a memory address?
Show answerHide answer
The number identifying one byte's position in memory.
What is the difference between a value and a reference to a value?
Show answerHide answer
A value is the data itself; a reference is the address saying where the data is.
Why could a 32-bit computer only use 4 GB of RAM?
Show answerHide answer
32-bit addresses can name only 2Β³Β² β 4.3 billion bytes = 4 GB.
In little-endian order, how is the 4-byte number 0x000000C8 laid out, starting from the first
address?
Show answerHide answer
C8 00 00 00 β smallest byte first.