Week 1 Review — Types, Memory, and Immutability
Goal
This review checks whether Week 1 is a picture I can say, not a page I can reread.
Week 1 topics:
- JDK vs JVM
- Stack vs heap
- Primitives vs wrappers
- Boxing and the
Integercache - Pass-by-value
- Identity vs equality
finalvs immutability- Records as values
- Shared singleton beans on the heap
1. Week 1 big picture
Thread stack (short-lived) Heap (shared)
┌─────────────────────┐ ┌──────────────────────────┐
│ place(orderReq) │ │ OrderService (singleton)│
│ quantity = 2 │ │ orders ─────────────────┼──▶ OrderRepository
│ orderReq ──────────┼────────────▶│ CreateOrderRequest DTO │
└─────────────────────┘ │ Money / records │
└──────────────────────────┘
- Locals live in the frame.
- Objects live on the heap.
- The service is one object for every request thread.
- Request data must not become a field on that service.
2. Core memory sentences
I compile with the JDK. I run bytecode on a JVM.
Stacks are per-thread frames. The heap is shared objects.
Primitives are values and never
null. Wrappers are objects and can benull.
Java copies the value; for objects that value is the reference.
==is identity.equalsis meaning.
finalstops reassignment; immutability also stops nested mutation.
Records for DTOs and values. Classes for entities and Spring beans.
A Spring singleton is one heap object, many stacks.
3. Speak these without notes
- Is Java pass-by-value? Prove it with reassignment vs
setName. - Why can
Integer==be true for 127 and false for 128? - Why is a mutable field on
@Servicea bug? - How do I make a type immutable? Where do records fall short?
- Why is a JPA entity not a record?
If any answer takes more than about 90 seconds or misses the trap, reread that day.
4. Tiny code proofs
Proof A — reassignment does not escape
static void rebind(StringBuilder sb) {
sb = new StringBuilder("nope");
}
StringBuilder sb = new StringBuilder("yes");
rebind(sb);
// sb still "yes"
Proof B — mutation does escape
static void append(StringBuilder sb) {
sb.append("!");
}
append(sb);
// sb is "yes!"
Proof C — unbox null
Integer n = null;
// int x = n; // NPE
5. Common mix-ups from this week
| Mix-up | Clear line |
|---|---|
| Stack holds objects | Stack holds frames and references; objects are on the heap |
final = immutable | final = no rebinding |
== on wrappers | Value compare with equals |
| Record = always safe to share | Copy mutable components |
| Singleton = one thread | Singleton = one instance, many threads |
6. Interview drill
Open Java fundamentals and answer out loud:
- How does Java pass arguments?
- How do you make a class immutable?
- What does
finalmean on a class, method, and variable? ==vsequalsfor wrappers- Records vs JavaBeans vs Lombok
Use the four-part answer.
7. Ready for Week 2?
I am ready if I can draw the stack/heap picture and explain pass-by-value with one example.
Week 2 starts at the class: the template those heap objects are built from.