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:
- Where Java stores variables and objects.
- Why copying an object variable does not copy the object.
- 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.
| Room | Picture | What it holds |
|---|---|---|
| Stack | A pile of trays | The variables of the methods that are running |
| Heap | A big warehouse | Every object made with new |
| Reference | An address ticket | A 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.
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".
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);
}
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:
| Code | What is copied | The result |
|---|---|---|
int b = a; | The value, which is 5 | a and b have their own boxes. Changing b does not change a. |
Kue k2 = k1; | The ticket, not the cake | k1 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:
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);
}
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:
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));
}
true
false
false
false
true
== compares tickets, not the content of the cakes.
k1 == k2istrue, because both hold a ticket to the same cake.k1 == k3isfalse. The content is the same, butk3was made with its ownnew. Two different cakes have two different addresses.
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.
| Use | For |
|---|---|
== | 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".
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);
}
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:
class Kue {
String rasa;
}
void main() {
Kue kue = null;
IO.println(kue.rasa);
}
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
- The stack is a pile of trays for the variables of methods. The heap is a warehouse for objects.
- An object variable holds an address ticket called a reference. Copying it copies the ticket, not the object.
==compares tickets..equals()compares content.
Challenge
Look at this code:
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.

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.


