String
23 interview questions and answers in the String section.
Questions in this section
- How String Pool Works
- Difference Between Creating String via Literal and via new
- When to Use intern()?
- Why String is Immutable
- When to Use StringBuilder vs StringBuffer?
- Why StringBuffer is Slower than StringBuilder?
- What Happens When Concatenating Strings with + Operator?
- How Java Compiler Optimizes String Concatenation?
- Can You Use == to Compare Strings?
- Difference Between == and equals() for String
- Where is String Pool Stored (Which Memory Area)?
- Can String Pool Cause OutOfMemoryError?
- What substring() Does and How It Worked Before Java 7
- Why substring() Implementation Was Changed in Java 7?
- How split() Method Works
- Difference Between replace() vs replaceAll()
- What is String Encoding?
- How to Properly Convert String to byte[] and Back
- What are Compact Strings in Java 9+?
- How to Find Out How Much Memory a String Occupies
- Can You Modify String Contents via Reflection?
- What is String Deduplication in G1 GC?
- Why String implements Comparable and CharSequence?
Study navigator
23 questions for Middle Java Developer interview preparation.
π All Questions
πΊοΈ 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 β β β β β
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
π― Recommended Study Order
π’ 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 complexreplace()β 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
@Stableannotation, 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