Muhammad Fari Madyan
Hire me
Java basics · part 13 of 21

Learn Java from Scratch Part 13: Stack, Heap and References — Address Tickets, == & equals

The thirteenth part of a Java series: the stack as a pile of trays, the heap as a warehouse, a reference as an address ticket, why copying an object variable does not copy the object, == versus equals, a shared wallet, and NullPointerException.

Muhammad Fari Madyan

· 8 min read

Baca dalam Bahasa Indonesia →

Introduction: Two Cakes That Are "Not Equal"

Two cakes. Same flavour, made in the same way. Ask Java: are they equal?

The answer is false.

Java is not lying. We just do not know yet where those cakes are stored. In this part we enter Java's engine room: the stack, the heap and references.

After this part, you will know:

  1. Where Java stores variables and objects.
  2. Why copying an object variable does not copy the object.
  3. When to use == and when to use .equals().

Answer to the Part 12 Challenge

The question: the constructor of Titik writes x = x; without this. What is the output?

The answer is A: 0.

Without this, both x mean the parameter. The ingredient goes back into its own funnel. The field x of the point is never filled, so it keeps its default value, which is 0.

If you answered A, well done!


1. The Problem: The Box Picture Is Not Complete

Since Part 3 we have pictured a variable as a box in the memory lockers. For int and double, that picture is correct.

But an object such as a cake can be big, and it can be used in many places at the same time. Java stores it in a different way.

If we do not understand how, we will fall into confusing bugs.


2. Two Rooms: A Stack of Trays and a Warehouse

Java's memory has two main rooms.

Room one: the stack, a pile of trays. Every time a method is called, a new tray is put on top of the pile. That tray holds the variables of that method. When the method ends, its tray is taken away.

Remember the room with a door in Part 10? That room is this tray.

Room two: the heap, a big warehouse. Every time we write new, a new object is made and stored in the warehouse.

The variable on the tray does not hold the cake. It holds a ticket with the address of the cake in the warehouse. This ticket is called a reference.

RoomPictureWhat it holds
StackA pile of traysThe variables of the methods that are running
HeapA big warehouseEvery object made with new
ReferenceAn address ticketA pointer from a variable on the stack to an object on the heap

So Kue k1 = new Kue("Cokelat"); means: make a cake in the warehouse, then keep its address ticket in the box k1.

The stack holds variables and tickets, the heap holds objects; two tickets can point to the same cake

Click the diagram to open it in full size.


3. A Look into Memory: Copying a Value and Copying a Ticket

Create a file named Gudang.java. Gudang means "warehouse".

JAVA
class Kue {
    String rasa;

    Kue(String rasa) {
        this.rasa = rasa;
    }
}

void main() {
    int a = 5;
    int b = a;
    b = 10;
    IO.println("a = " + a + ", b = " + b);

    Kue k1 = new Kue("Cokelat");
    Kue k2 = k1;
    k2.rasa = "Keju";
    IO.println("k1: " + k1.rasa + ", k2: " + k2.rasa);
}
TEXT
a = 5, b = 10
k1: Keju, k2: Keju

The second line may surprise you. We only changed k2, but k1 changed too. Let us follow it step by step:

CodeWhat is copiedThe result
int b = a;The value, which is 5a and b have their own boxes. Changing b does not change a.
Kue k2 = k1;The ticket, not the cakek1 and k2 point to the same cake in the warehouse.

So when the flavour is changed through k2, the only cake in the warehouse changes. When we read through k1, we see that same cake: its flavour is now cheese.

This also completes the photocopy story from Part 10. What goes into a funnel is always a copy. But when the copy is a ticket, the machine can use that ticket to change the real cake:

JAVA
class Kue {
    String rasa;

    Kue(String rasa) {
        this.rasa = rasa;
    }
}

void gantiRasa(Kue k) {
    k.rasa = "Stroberi";
}

void main() {
    Kue kue = new Kue("Cokelat");
    gantiRasa(kue);
    IO.println(kue.rasa);
}
TEXT
Stroberi

Ganti rasa means "change the flavour", and stroberi is strawberry.


4. == Compares Tickets, .equals() Compares Content

Now we solve the mystery from the introduction. Create a file named Banding.java ("compare"), with the same Kue class:

JAVA
void main() {
    Kue k1 = new Kue("Cokelat");
    Kue k2 = k1;
    Kue k3 = new Kue("Cokelat");

    IO.println(k1 == k2);
    IO.println(k1 == k3);
    IO.println(k1.equals(k3));

    String a = "rani";
    String b = "RANI".toLowerCase();
    IO.println(a == b);
    IO.println(a.equals(b));
}
TEXT
true
false
false
false
true

== compares tickets, not the content of the cakes.

  • k1 == k2 is true, because both hold a ticket to the same cake.
  • k1 == k3 is false. The content is the same, but k3 was made with its own new. Two different cakes have two different addresses.

== compares the addresses on the tickets, equals compares the content of the objects

Click the diagram to open it in full size.

So why is k1.equals(k3) also false? Because Kue is our own class, and we have never taught Java how to compare the content of two cakes. Without that lesson, .equals() only compares tickets, just like ==. How to teach it is in Part 16, after we learn to rewrite a method.

String is different. The String class already knows how to compare its letters. So a.equals(b) is true.

This is the answer we promised in Part 4. a and b both hold rani, but they are two different objects in the warehouse. == sees two different addresses, so the result is false.

UseFor
==Primitive types such as int, or checking if two tickets point to the same object
.equals()Comparing content, for example two String values

One more story. A cake in the warehouse that no ticket points to any more is cleaned up automatically by Java's "cleaner", which is called the garbage collector. You do not need to delete objects yourself.


5. A Real Example: A Shared Wallet

Create a file named Dompet.java. Dompet means "wallet" and isi means "content".

JAVA
class Dompet {
    int isi;

    Dompet(int isi) {
        this.isi = isi;
    }
}

void main() {
    Dompet milikRani = new Dompet(100_000);
    Dompet milikBudi = milikRani;

    milikBudi.isi -= 30_000;

    IO.println("Dompet Rani: " + milikRani.isi);
    IO.println("Dompet Budi: " + milikBudi.isi);
}
TEXT
Dompet Rani: 70000
Dompet Budi: 70000

Rani makes a wallet with 100,000 rupiah and gives its ticket to Budi. Budi spends 30,000. When Rani checks her wallet, only 70,000 is left.

They do not have two wallets. They hold two tickets to the same wallet.

In a real app, this can be on purpose. But when it is not on purpose, it becomes a bug that is hard to find.


6. Three Reference Traps

Trap one: comparing strings with ==.

As in section 4, == can give false even when the text is the same. To compare the content of strings, always use .equals().

Trap two: thinking that = copies an object.

k2 = k1 only copies the ticket. There is still one cake. If you need a second cake, make it with new.

Trap three: an empty ticket.

An object variable can hold null. It means the ticket points nowhere yet. If you use that empty ticket, the program stops:

JAVA
class Kue {
    String rasa;
}

void main() {
    Kue kue = null;
    IO.println(kue.rasa);
}
TEXT
Exception in thread "main" java.lang.NullPointerException: Cannot read field "rasa" because "<local1>" is null
	at Kosong.main(Kosong.java:7)

The message says: it cannot read the field rasa because the ticket is null. <local1> is how Java names the first local variable in main, which is kue. The address is still clear: the file Kosong.java, line 7.

For now, make sure every object variable is filled with new before you use it.


Summary

  1. The stack is a pile of trays for the variables of methods. The heap is a warehouse for objects.
  2. An object variable holds an address ticket called a reference. Copying it copies the ticket, not the object.
  3. == compares tickets. .equals() compares content.

Challenge

Look at this code:

JAVA
void main() {
    int[] a = {1, 2, 3};
    int[] b = a;
    b[0] = 99;
    IO.println(a[0]);
}

What is the output?

  • A. 1
  • B. An error.
  • C. 99

A hint: remember that an array is also made in the warehouse. I explain the answer at the start of Part 14.


Conclusion & What's Next

You now know what an object variable really holds: not the cake, but a ticket to the cake. If you remember only one thing, remember the shared wallet. Two tickets can point to one and the same object.

In Part 14, we protect the content of an object from careless hands. There is a vault, a service counter, and rules about who may touch what.

See you in Part 14. Get your coffee ready. Hot chocolate is fine too.

ShareLinkedInXWhatsApp

Written by Muhammad Fari Madyan

Senior Backend Engineer and co-founder of LampungDev. I write up what I build so others can learn from it.

Work with me
← part 12Constructors and thispart 14 →Encapsulation

Muhammad Fari Madyan

Senior Backend Engineer building real-time, high-concurrency systems that hold up in production. Co-founder of LampungDev. Based in Bandar Lampung, Indonesia (UTC+7), working remote.

Work

Case studiesCahTixExam platform, 4 stacksOpen source

© 2026 Muhammad Fari Madyan

Designed and built by Muhammad Fari Madyan