Zum Hauptinhalt springen

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:

  1. JDK vs JVM
  2. Stack vs heap
  3. Primitives vs wrappers
  4. Boxing and the Integer cache
  5. Pass-by-value
  6. Identity vs equality
  7. final vs immutability
  8. Records as values
  9. 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 be null.

Java copies the value; for objects that value is the reference.

== is identity. equals is meaning.

final stops 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

  1. Is Java pass-by-value? Prove it with reassignment vs setName.
  2. Why can Integer == be true for 127 and false for 128?
  3. Why is a mutable field on @Service a bug?
  4. How do I make a type immutable? Where do records fall short?
  5. 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-upClear line
Stack holds objectsStack holds frames and references; objects are on the heap
final = immutablefinal = no rebinding
== on wrappersValue compare with equals
Record = always safe to shareCopy mutable components
Singleton = one threadSingleton = 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 final mean on a class, method, and variable?
  • == vs equals for 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.

Next: Week 2 Day 1 — Class versus Object