Immutability
29 interview questions and answers in the Immutability section.
Questions in this section
- What is an immutable (unchangeable) object?
- What advantages do immutable objects provide?
- How to create an immutable class in Java?
- Why is the String class immutable?
- What are the consequences of String immutability?
- Why are immutable objects thread-safe?
- What is the final keyword and how does it help create immutable classes?
- Is it enough to make all fields final for immutability?
- What to do if a class field of an immutable class references a mutable object?
- What is a defensive copy?
- When should you make a defensive copy?
- How to protect a collection from modification?
- What is Collections.unmodifiableList() and how does it work?
- What is the difference between shallow copy and deep copy?
- Can you inherit from an immutable class?
- Why should an immutable class be final?
- What happens if you override getter in subclass of immutable class?
- How does String pool work and how is it related to immutability?
- Can you change String value via reflection?
- What is Record and how does it help create immutable classes?
- Why are LocalDate, LocalDateTime immutable?
- What are the advantages of immutable objects for caching?
- How does immutability affect performance?
- What are persistent data structures?
- Are there disadvantages to immutable objects?
- How to implement Builder pattern for an immutable class?
- Can you use immutable objects as keys in HashMap?
- What happens if you modify a mutable key in HashMap?
- How to properly work with collections in immutable classes?
Study navigator
29 questions for Middle Java Developer interview preparation.
All Questions
Topic Dependency Map
┌──────────────────────────────────────────┐
│ IMMUTABILITY BASICS (1-3) │
│ 1. What is immutable │
│ 2. Advantages │
│ 3. How to create │
└──────────────────┬───────────────────────┘
│
┌──────────────────────────┼──────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ STRING & DATE │ │ FINAL & │ │ DEFENSIVE COPY │
│ (4-5, 18, 21) │ │ INHERITANCE │ │ & COPYING │
│ 4. String immut│ │ (7-8, 15-17) │ │ (9-14, 29) │
│ 5. Consequences│ │ 7. final │ │ 9. Mutable fields │
│ 18. String pool│ │ 8. All final? │ │ 10. Defensive copy │
│ 21. LocalDate │ │ 15. Inheritance│ │ 11. When to copy │
│ │ │ 16. Final class│ │ 12. Protect coll. │
└───────┬───────┘ │ 17. Getter │ │ 13. unmodifiable │
│ └───────┬───────┘ │ 14. Shallow vs Deep│
│ │ │ 29. Collections │
└────────────────────────┼────────────────┴────────┬───────────┘
│ │
┌────────────────────────┼────────────────────────┘
▼ ▼
┌───────────────┐ ┌───────────────┐
│ THREAD-SAFE │ │ ADVANCED │
│ & CACHING │ │ (19-20, 24-28) │
│ (6, 22-23) │ │ 19. Reflection │
│ 6. Thread-safe │ │ 20. Record │
│ 22. Caching │ │ 24. Persistent │
│ 23. Perform. │ │ 25. Disadvant. │
│ │ │ 26. Builder │
│ │ │ 27. HashMap key│
│ │ │ 28. Mutable mut.│
└───────────────┘ └─────────────────┘
Recommended Study Order
Level: Basic (Weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Basics | Q1, Q2 | What is immutable, why it matters |
| 2 | Creating | Q3 | How to create an immutable class |
| 3 | final | Q7 | Three contexts of final |
| 4 | String | Q4, Q5 | Why String is immutable, consequences |
| 5 | Thread-safe | Q6 | Why immutable = thread-safe |
Level: Intermediate (Weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Defensive copy | Q9, Q10, Q11 | Copying mutable fields |
| 2 | Collections | Q12, Q13, Q29 | unmodifiableList, List.copyOf |
| 3 | Copy types | Q14 | Shallow vs deep copy |
| 4 | Builder | Q26 | Pattern for complex immutable objects |
| 5 | Record | Q20 | Record vs Lombok @Value vs final class |
| 6 | LocalDate | Q21 | Why Java Time is immutable |
Level: Advanced (Weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Reflection attack | Q19 | Can you break immutability, module system |
| 2 | Inheritance | Q15, Q16, Q17 | Inheritance, sealed classes, overridden getters |
| 3 | Caching | Q22 | Why immutable objects are ideal for caching |
| 4 | Performance | Q23 | Trade-offs, allocation vs contention |
| 5 | Persistent DS | Q24 | Structural sharing, Vavr |
| 6 | HashMap keys | Q27, Q28 | Immutable keys, mutable key mutation bug |
Key Topic Connections
Topic: Basics
Q1 (Immutable) → Q2 (Advantages) → Q3 (How to create)
Topic: final and inheritance
Q7 (final) → Q8 (All final?) → Q15 (Inheritance) → Q16 (Final class) → Q17 (Getter)
Key connections:
- Q7 ↔ Q8: final fields ≠ immutable class (mutable references)
- Q15 ↔ Q16: final class prohibits inheritance, sealed — restricts
- Q17 ↔ Q29: Getter returns a mutable object — violation of immutable contract
Topic: Defensive Copy and collections
Q9 (Mutable fields) → Q10 (Defensive copy) → Q11 (When to copy)
↓
Q12 (Protect collection) → Q13 (unmodifiableList) → Q14 (Shallow vs Deep) → Q29 (Collections)
Topic: String and Date
Q4 (String immutable) → Q5 (Consequences) → Q18 (String Pool)
Q21 (LocalDate immutable)
Topic: Advanced
Q19 (Reflection) → Q20 (Record) → Q24 (Persistent DS)
Q27 (HashMap keys) → Q28 (Mutable key mutation)
Quick Reference: What to Know at Each Level
Basic
- Immutable = state doesn’t change after creation
- Advantages: thread-safe, cacheable, predictable
- final for class, fields, methods — three different meanings
- String immutable — security, String Pool, hashing
Intermediate
- Defensive copy: copy mutable fields in constructor and getters
List.copyOf()vsCollections.unmodifiableList()— independent copy vs live wrapper- Shallow copy = copies container, deep copy = recursively entire graph
- Record = immutable value class with minimal boilerplate
- Builder pattern for objects with 4+ fields
Advanced
- Reflection on final fields is blocked (Java 16+, JEP 396)
- Sealed classes (Java 17) as an alternative to final for immutable hierarchies
- Persistent data structures: structural sharing, O(log n) modification
- Mutable key in HashMap = logical inaccessibility (not a leak!)
- Immutable objects = linear scaling on read-heavy workloads
- String Pool is tied to immutability: one object → many references safely
Format of Each File
Each file contains:
- Basic — fundamental understanding, simple analogies, examples
- Intermediate — internals, common mistakes, practical examples
- Advanced — deep dive, edge cases, production experience, monitoring
- Interview Cheat Sheet — key points, frequent questions, red flags, related topics