Section 12 Β· 23 questions

String

23 interview questions and answers in the String section.

English String Source Markdown
Language versions: English Russian Ukrainian

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 Java Developer interview preparation.


πŸ“‹ All Questions

# Question Difficulty
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 ⭐⭐

πŸ—ΊοΈ Topic Dependency Map

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   STRING POOL AND CREATION (1-3)         β”‚
                    β”‚   1. String Pool                         β”‚
                    β”‚   2. Literal vs new                      β”‚
                    β”‚   3. intern()                            β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                       β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β–Ό                          β–Ό                          β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ IMMUTABILITY   β”‚        β”‚ CONCATENATION  β”‚        β”‚ COMPARISON         β”‚
    β”‚ AND MODIFI     β”‚        β”‚ (7-8)          β”‚        β”‚ (9-10)             β”‚
    β”‚ CATION (4-6)   β”‚        β”‚ 7. + Operator  β”‚        β”‚ 9. == for String   β”‚
    β”‚ 4. Why immut   β”‚        β”‚ 8. Compiler    β”‚        β”‚ 10. == vs equals   β”‚
    β”‚ 5. StringBuilderβ”‚       β”‚    Optimiz.    β”‚        β”‚                    β”‚
    β”‚ 6. StringBuffer β”‚       β”‚                β”‚        β”‚                    β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚                        β”‚                        β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                     β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   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 Q1, Q2 How strings are created, literals vs new
2 StringBuilder vs StringBuffer Q5, Q6 When to use which
3 Concatenation + Q7 What happens under the hood
4 == vs equals Q9, 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() Q3 When it saves memory, when it hurts
2 Immutability Q4 Why String is immutable, the cost
3 Compiler Optimizations Q8 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

Q1 (String Pool) β†’ Q2 (Literal vs new) β†’ Q3 (intern())
     ↓
Q11 (Where stored) β†’ Q12 (OOM from String Pool)

Key connections:

  • Q1 ↔ Q11: String Pool is stored in Java Heap (since Java 7u6)
  • Q3 ↔ Q12: Mass intern() β†’ StringTable overflow β†’ OOM
  • Q2 ↔ Q3: new String("...").intern() vs "..." β€” when it makes sense

Topic: Immutability and Modification

Q4 (Immutability) β†’ Q5 (StringBuilder vs StringBuffer) β†’ Q6 (Why StringBuffer is slower)

Key connections:

  • Q4 ↔ Q5: Immutability = each concat = new object β†’ StringBuilder for modifications
  • Q5 ↔ Q6: synchronized overhead, biased locking removal (Java 15+)

Topic: Concatenation

Q7 (+ Operator) β†’ Q8 (Compiler Optimization)

Key connections:

  • Q7 ↔ Q8: Java 8 β†’ StringBuilder, Java 9+ β†’ invokedynamic (JEP 280)

Topic: Comparison

Q9 (==) β†’ Q10 (== vs equals)

Key connections:

  • Q9 ↔ Q10: == compares references, equals() β€” content

Topic: Memory and Internals

Q19 (Compact Strings) β†’ Q20 (String Size) β†’ Q21 (Reflection) β†’ Q22 (Deduplication)

Key connections:

  • Q19 ↔ Q20: coder field determines Latin1 (1 byte/char) vs UTF-16 (2 bytes/char)
  • Q20 ↔ Q22: Deduplication saves memory on duplicate Strings
  • Q21 ↔ Q4: Reflection can break immutability (but blocked by module system)

Topic: Methods and Encodings

Q13 (substring) β†’ Q14 (substring Java 7 fix)
Q15 (split) β†’ Q16 (replace vs replaceAll)
Q17 (Encoding) β†’ Q18 (String ↔ byte[])

πŸŽ“ Cheat Sheet: What to Know for Each Level

🟒 Junior

  • String is immutable β€” cannot be modified after creation
  • Literals = from pool, new String() = new object
  • StringBuilder for modifications, StringBuffer for multithreading
  • == compares references, equals() β€” content
  • + is ok for 1-2 strings, for loops β€” use StringBuilder

🟑 Middle

  • String Pool in Java Heap (since Java 7u6), intern() saves memory on duplicates
  • Compact Strings (Java 9+): Latin1 (1 byte/char) for ASCII, UTF-16 for others
  • split() β€” Fast Path for simple delimiters, regex for complex
  • replace() β€” literal replacement, replaceAll() β€” regex
  • Default encoding depends on OS β€” always specify explicitly

πŸ”΄ Senior

  • StringTable β€” native hash table, resizable since JDK 11 (JEP 341)
  • substring Java 6: shared char[] (memory leak), Java 7+: array copy
  • invokedynamic for concatenation (JEP 280, Java 9+), StringConcatFactory
  • @Stable annotation, coder field, SIMD optimizations for equals()
  • Reflection on String blocked by module system (Java 9+, enforced Java 16+)
  • G1 String Deduplication: saves 10-30% memory on duplicates, not for ZGC

πŸ“ File Format

Each file contains:

  • 🟒 Junior Level β€” basic understanding, simple analogies, examples
  • 🟑 Middle Level β€” internals, typical mistakes, practical examples
  • πŸ”΄ Senior Level β€” deep dive, edge cases, production experience, monitoring
  • 🎯 Interview Cheat Sheet β€” key points, frequent questions, red flags, related topics