🔤 Section 12 · 23 questions

String

23 interview questions and answers in the String section.

Questions in this section

  1. How String Pool Works
  2. Difference Between Creating String via Literal and via new
  3. When to Use intern()
  4. Why String is Immutable
  5. When to Use StringBuilder vs StringBuffer
  6. Why StringBuffer is Slower than StringBuilder
  7. What Happens When Concatenating Strings with + Operator
  8. How Java Compiler Optimizes String Concatenation
  9. Can You Use == to Compare Strings
  10. Difference Between == and equals() for String
  11. Where is String Pool Stored (Which Memory Area)
  12. Can String Pool Cause OutOfMemoryError
  13. What substring() Does and How It Worked Before Java 7
  14. Why substring() Implementation Was Changed in Java 7
  15. How split() Method Works
  16. Difference Between replace() vs replaceAll()
  17. What is String Encoding
  18. How to Properly Convert String to byte[] and Back
  19. What are Compact Strings in Java 9+
  20. How to Find Out How Much Memory a String Occupies
  21. Can You Modify String Contents via Reflection
  22. What is String Deduplication in G1 GC
  23. Why String implements Comparable and CharSequence

Study navigator

23 questions for Middle & Senior Java Developer interview preparation.


📋 All Questions

# Question Difficulty
01 How String Pool Works ⭐⭐
02 Difference Between Creating String via Literal and via new ⭐
03 When to Use intern() ⭐⭐⭐
04 Why String is Immutable ⭐⭐
05 When to Use StringBuilder vs StringBuffer ⭐
06 Why StringBuffer is Slower than StringBuilder ⭐⭐
07 What Happens When Concatenating Strings with + Operator ⭐⭐
08 How Java Compiler Optimizes String Concatenation ⭐⭐⭐
09 Can You Use == to Compare Strings ⭐
10 Difference Between == and equals() for String ⭐⭐
11 Where is String Pool Stored (Which Memory Area) ⭐⭐⭐
12 Can String Pool Cause OutOfMemoryError ⭐⭐⭐
13 What substring() Does and How It Worked Before Java 7 ⭐⭐
14 Why substring() Implementation Was Changed in Java 7 ⭐⭐
15 How split() Method Works ⭐⭐
16 Difference Between replace() vs replaceAll() ⭐⭐
17 What is String Encoding ⭐⭐
18 How to Properly Convert String to byte[] and Back ⭐⭐
19 What are Compact Strings in Java 9+ ⭐⭐⭐
20 How to Find Out How Much Memory a String Occupies ⭐⭐⭐
21 Can You Modify String Contents via Reflection ⭐⭐⭐
22 What is String Deduplication in G1 GC ⭐⭐⭐
23 Why String implements Comparable and CharSequence ⭐⭐

🗺️ Topic Dependency Map

                    ┌──────────────────────────────────────────┐
                    │   STRING POOL AND CREATION (01-03)       │
                    │   01. String Pool                        │
                    │   02. Literal vs new                     │
                    │   03. intern()                           │
                    └──────────────────┬───────────────────────┘
                                       │
            ┌──────────────────────────┼──────────────────────────┐
            ▼                          ▼                          ▼
    ┌───────────────┐        ┌───────────────┐        ┌────────────────────┐
    │ IMMUTABILITY   │        │ CONCATENATION  │        │ COMPARISON         │
    │ AND MODIFI-    │        │ (07-08)        │        │ (09-10)            │
    │ CATION (04-06) │        │ 07. + Operator │        │ 09. == for String  │
    │ 04. Why immut  │        │ 08. Compiler   │        │ 10. == vs equals   │
    │ 05. StringBld  │        │    Optimiz.    │        │                    │
    │ 06. StringBuf  │        │                │        │                    │
    └───────┬───────┘        └───────┬───────┘        └────────┬───────────┘
            │                        │                        │
            └────────────────────────┼────────────────────────┘
                                     ▼
                    ┌──────────────────────────────────────────┐
                    │   MEMORY AND INTERNALS (11-12, 19-22)    │
                    │   11. Where String Pool is stored        │
                    │   12. OOM from String Pool               │
                    │   19. Compact Strings (Java 9+)          │
                    │   20. String memory size                 │
                    │   21. Reflection on String               │
                    │   22. String Deduplication (G1 GC)       │
                    └──────────────────────────────────────────┘
                                     │
            ┌────────────────────────┼────────────────────────┐
            ▼                        ▼                        ▼
    ┌───────────────┐        ┌───────────────┐        ┌────────────────────┐
    │ METHODS        │        │ ENCODINGS      │        │ INTERFACES         │
    │ (13-16)        │        │ (17-18)        │        │ (23)               │
    │ 13. substring  │        │ 17. Encoding   │        │ 23. Comparable +   │
    │ 14. substring  │        │ 18. String ↔   │        │     CharSequence   │
    │     Java 7 fix │        │     byte[]     │        │                    │
    │ 15. split      │        │                │        │                    │
    │ 16. replace    │        │                │        │                    │
    └───────────────┘        └───────────────┘        └────────────────────┘

🟢 Junior Level (Weeks 1-2)

Step Topic Files Goal
1 String Pool Q01, Q02 How strings are created, literals vs new
2 StringBuilder vs StringBuffer Q05, Q06 When to use which
3 Concatenation + Q07 What happens under the hood
4 == vs equals Q09, Q10 Why == is not for content comparison
5 substring/split/replace Q13, Q15, Q16 Basic string manipulation methods

🟡 Middle Level (Weeks 3-4)

Step Topic Files Goal
1 intern() Q03 When it saves memory, when it hurts
2 Immutability Q04 Why String is immutable, the cost
3 Compiler Optimizations Q08 Constant folding, invokedynamic
4 Encodings Q17, Q18 UTF-8, UTF-16, conversion to byte[]
5 Memory Q20 How many bytes a String occupies

🔴 Senior Level (Weeks 5-6)

Step Topic Files Goal
1 String Pool Internals Q11, Q12 StringTable, OOM, resizing
2 Compact Strings Q19 Latin1 vs UTF-16, coder field, SIMD
3 substring Java 6→7 Q13, Q14 Shared array memory leak, why changed
4 Reflection on String Q21 setAccessible, module system, dangers
5 String Deduplication Q22 G1 GC, ZGC, @Stable, thread safety
6 Comparable + CharSequence Q23 HashMap tree bins, CharSequence contract

🔗 Key Topic Connections

Topic: String Pool

Q01 (String Pool) ──► Q02 (Literal vs new) ──► Q03 (intern())
      │
      ▼
Q11 (Where stored) ──► Q12 (OOM from String Pool)
  • Q01 ↔ Q11: String Pool resides in Java Heap (since Java 7u6).
  • Q03 ↔ Q12: Unbounded intern() calls $\to$ native StringTable bucket collision $\to$ performance collapse and OOM.
  • Q02 ↔ Q03: new String("...").intern() creates an unnecessary heap garbage object before resolving against the pool.

Topic: Immutability & Modification

Q04 (Immutability) ──► Q05 (StringBuilder vs StringBuffer) ──► Q06 (Why StringBuffer is slower)
  • Q04 ↔ Q05: String immutability allocates new objects on every modification; StringBuilder avoids allocation via mutable internal arrays.
  • Q05 ↔ Q06: StringBuffer methods synchronize on this, causing monitor contention overhead.

Topic: Concatenation

Q07 (+ Operator) ──► Q08 (Compiler Optimization)
  • Q07 ↔ Q08: Java 8 used StringBuilder; Java 9+ compiles + to invokedynamic calling StringConcatFactory (JEP 280).

Topic: Memory & Low-Level Internals

Q19 (Compact Strings) ──► Q20 (String Size) ──► Q21 (Reflection) ──► Q22 (Deduplication)
  • Q19 ↔ Q20: coder byte flag selects LATIN1 (1 byte/char) vs UTF16 (2 bytes/char).
  • Q20 ↔ Q22: G1 String Deduplication re-points identical backing byte[] arrays to a single shared array during GC.
  • Q21 ↔ Q04: Java Module System (JEP 261 / JEP 396) strictly encapsulates internal fields, preventing setAccessible(true) on String.value.

🎓 Cheat Sheet: What to Know for Each Level

🟢 Junior

  • String is immutable — its character contents cannot change after creation.
  • Literals reference the String Pool; new String(...) allocates a distinct heap object.
  • Use StringBuilder for non-thread-safe modifications in loops; StringBuffer is synchronized.
  • == checks reference identity; .equals() checks character content equality.
  • Use + for simple 1-2 item concatenation; use StringBuilder inside loops.

🟡 Middle

  • String Pool is located in the Java Heap (since Java 7u6); intern() ensures unique string instances.
  • Compact Strings (Java 9+): Uses byte[] with a coder flag (LATIN1 = 1 byte/char, UTF16 = 2 bytes/char).
  • split() uses Fast Path for single-character regex delimiters, avoiding pattern compilation.
  • replace() does literal string replacement; replaceAll() compiles a regex pattern.
  • Always pass explicit StandardCharsets.UTF_8 when converting strings to byte[].

🔴 Senior

  • HotSpot StringTable: Native C++ hash table, resizable since JDK 11 (JEP 341).
  • substring() memory leak fix: Java 6 shared the backing char[]; Java 7+ creates a defensive copy.
  • invokedynamic concatenation (JEP 280): StringConcatFactory.makeConcatWithConstants optimizes bytecode at runtime.
  • @Stable annotation, coder field, and SIMD vector instructions for hardware-accelerated equals() and hashing.
  • Reflection attacks on String.value blocked by Strong Encapsulation (Java 16+ JEP 396).
  • G1 GC String Deduplication (-XX:+UseStringDeduplication): Reclaims duplicate byte[] without altering object headers.