Are there disadvantages to immutable objects?
Yes, immutability has downsides.
Junior Level
Yes, immutability has downsides.
Disadvantages
- Memory consumption — each modification creates a new object
- Slower with frequent updates — data needs to be copied
- More code — need to write constructors,
equals,hashCode,toString
Example of the problem
String s = "";
for (int i = 0; i < 10000; i++) {
s += i; // 10000 objects created!
}
Solution
Use StringBuilder for frequent modifications and immutable objects for storage.
Middle Level
1. Performance with frequent updates
For scenarios with per-second modification of one field in a large object:
- Constant allocation of new objects → load on Young Gen
- GC cannot always hide pauses (GC Jitter)
- Example: game logic (coordinates of 1000 units)
2. Memory consumption
Storing many intermediate versions of long-lived objects (e.g., in a cache) bloats Old Generation. Without structural sharing, RAM consumption grows linearly.
3. Boilerplate before Java Records
Before Records, creating an immutable class required:
- Multiple
finalfields - Verbose constructors
- Manual
equals,hashCode,toString - Builder pattern for convenient creation
4. Deep immutability problem
Ensuring 100% immutability for a complex object graph is hard. One forgotten defensive copy destroys the entire class’s safety.
5. Inheritance restriction
Immutable classes must be final — this makes classic inheritance-based patterns impossible.
How to mitigate the disadvantages
- StringBuilder for concatenation in loops
- Builder pattern for creating complex objects
- Records (Java 16+) for minimal boilerplate
Senior Level
Interaction with frameworks
Hibernate/JPA — requires a no-arg constructor and setters for proxying and lazy loading. Truly immutable entities with Hibernate are a non-trivial task:
- Use
@Immutableannotation - Or work through DTOs
Jackson — requires @JsonCreator, @JsonProperty for deserialization into immutable objects/records.
Trade-offs
| Scenario | Immutability | Mutability |
|---|---|---|
| High Frequency Updates | Poor (GC pressure) | Good |
| Multi-threaded Read | Excellent (lock-free) | Poor (contention) |
| Framework Integration | Difficult | Simple |
| Bug Prevention | Excellent | Average |
Mitigation strategy
- Builder +
toBuilder()— convenient “modification” of immutable objects - Records — minimal boilerplate
- Persistent Collections (Vavr) — $O(\log n)$ instead of $O(n)$
- Combined approach — mutable for processing, immutable for storage
Summary for Senior
- The main disadvantage — allocation overhead in High Frequency Update scenarios
- Poor compatibility with legacy frameworks (Hibernate, JPA)
- To compensate: Builders, Records, Persistent Collections
- Immutability is a trade-off: safety vs. performance in specific cases
Interview Cheat Sheet
Must know:
- Disadvantages: GC pressure (frequent allocations), copying overhead, boilerplate before Records
- Deep immutability is hard — one forgotten copy destroys safety
- Inheritance restriction — immutable classes must be
final - Hibernate/JPA requires setters and no-arg constructor — difficult with immutable entities
- Jackson requires
@JsonCreator+@JsonPropertyfor deserialization - Mitigation: StringBuilder, Builder pattern, Records, Persistent Collections (Vavr)
Frequent follow-up questions:
- How to work with Hibernate? —
@Immutableannotation or work through DTOs - Jackson won’t deserialize Records? — Need
@JsonCreator+@JsonProperty(better in newer versions) - When does immutability hurt? — High Frequency Update: game logic, coordinates of 1000 units
- What is GC Jitter? — Minor GC pauses aren’t always predictable — latency spike
Red flags (do NOT say):
- “Immutability has no disadvantages” — GC pressure and boilerplate are real
- “Hibernate works great with immutable” — it requires setters, no-arg constructor
- “Records solve all problems” — collections and frameworks are still complex
- “Mutability is always faster” — in a multi-threaded environment without locks, immutability is faster
Related topics:
- [[23. How does immutability affect performance]]
- [[20. What is Record and how does it help create immutable classes]]
- [[26. How to implement Builder pattern for an immutable class]]
- [[24. What are persistent data structures]]