🔒 Section 13 · 29 questions
Immutability
29 interview questions and answers in the Immutability section.
Questions in this section
- What is an Immutable Object
- What Advantages Do Immutable Objects Provide
- How to Create an Immutable Class in Java
- Why is 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 Class Field References a Mutable Object
- What is Defensive Copy
- When Should You Make Defensive Copy
- How to Protect a Collection from Modification
- What is Collections.unmodifiableList() and How Does It Work
- 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 and 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
Comprehensive guide containing 29 core interview questions across Junior 🟢, Middle 🟡, and Senior 🔴 levels.
📋 All Questions
🗺️ Topic Dependency Map
┌──────────────────────────────────────────┐
│ IMMUTABILITY BASICS (01-03) │
│ 01. What is Immutable │
│ 02. Advantages │
│ 03. How to Create │
└──────────────────┬───────────────────────┘
│
┌──────────────────────────┼──────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ STRING & TIME │ │ FINAL & │ │ DEFENSIVE COPYING │
│ (04-05,18,21) │ │ INHERITANCE │ │ & COLLECTIONS │
│ 04. String │ │ (07-08, 15-17)│ │ (09-14, 29) │
│ 05. Conseq. │ │ 07. final │ │ 09. Mutable Fields │
│ 18. Str Pool │ │ 08. All final?│ │ 10. Defensive Copy │
│ 21. LocalDate │ │ 15. Inherit │ │ 11. When to Copy │
│ │ │ 16. Final cls │ │ 12. Protect Coll. │
└───────┬───────┘ │ 17. Getter │ │ 13. unmodifiable │
│ └───────┬───────┘ │ 14. Shallow vs Deep│
│ │ │ 29. Collections │
└────────────────────────┼────────────────└────────┬───────────┘
│ │
┌────────────────────────┼────────────────────────┘
▼ ▼
┌───────────────┐ ┌───────────────┐
│ THREAD SAFETY │ │ ADVANCED │
│ & CACHING │ │ (19-20, 22-28)│
│ (06, 22-23) │ │ 19. Reflection│
│ 06. ThreadSafe│ │ 20. Records │
│ 22. Caching │ │ 24. Persistent│
│ 23. Perform. │ │ 25. Tradeoffs │
│ │ │ 26. Builder │
│ │ │ 27. Map Keys │
│ │ │ 28. Mut. Key │
└───────────────┘ └───────────────┘
🎯 Recommended Study Tracks
🟢 Junior Track (Foundations)
- Step 1: Core Concepts — 01. What is an Immutable Object, 02. What Advantages Do Immutable Objects Provide
- Step 2: Basic Creation — 03. How to Create an Immutable Class in Java
- Step 3: The final Keyword — 07. What is the final Keyword and How Does It Help Create Immutable Classes
- Step 4: String & Thread Safety — 04. Why is String Class Immutable, 06. Why Are Immutable Objects Thread-Safe
🟡 Middle Track (Defensive Copying & Collections)
- Step 1: Defensive Copying — 09. What to Do If Class Field References a Mutable Object, 10. What is Defensive Copy, 11. When Should You Make Defensive Copy
- Step 2: Collections Protection — 12. How to Protect a Collection from Modification, 13. What is Collections.unmodifiableList() and How Does It Work, 29. How to Properly Work with Collections in Immutable Classes
- Step 3: Shallow vs Deep Copies — 14. Difference Between Shallow Copy and Deep Copy
- Step 4: Modern Java Idioms — 20. What is Record and How Does It Help Create Immutable Classes, 21. Why Are LocalDate and LocalDateTime Immutable, 26. How to Implement Builder Pattern for an Immutable Class
🔴 Senior Track (JMM, Memory, Reflection & Persistent DS)
- Step 1: JMM & Final Field Semantics — 06. Why Are Immutable Objects Thread-Safe, 08. Is It Enough to Make All Fields final for Immutability
- Step 2: Inheritance & Subclassing Attacks — 15. Can You Inherit From an Immutable Class, 16. Why Should an Immutable Class Be final, 17. What Happens If You Override Getter in Subclass of Immutable Class
- Step 3: Reflection & Strong Encapsulation — 19. Can You Change String Value via Reflection
- Step 4: Highload Performance & Caching — 22. What Are the Advantages of Immutable Objects for Caching, 23. How Does Immutability Affect Performance, 24. What Are Persistent Data Structures
- Step 5: Map Key Invariants — 27. Can You Use Immutable Objects as Keys in HashMap, 28. What Happens If You Modify a Mutable Key in HashMap