🔤 Section 12 · 23 questions
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 & Senior Java Developer interview preparation.
📋 All Questions
🗺️ 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 │ │ │ │ │
└───────────────┘ └───────────────┘ └────────────────────┘
🎯 Recommended Study Order
🟢 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$ nativeStringTablebucket 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;
StringBuilderavoids allocation via mutable internal arrays. - Q05 ↔ Q06:
StringBuffermethods synchronize onthis, causing monitor contention overhead.
Topic: Concatenation
Q07 (+ Operator) ──► Q08 (Compiler Optimization)
- Q07 ↔ Q08: Java 8 used
StringBuilder; Java 9+ compiles+toinvokedynamiccallingStringConcatFactory(JEP 280).
Topic: Memory & Low-Level Internals
Q19 (Compact Strings) ──► Q20 (String Size) ──► Q21 (Reflection) ──► Q22 (Deduplication)
- Q19 ↔ Q20:
coderbyte flag selectsLATIN1(1 byte/char) vsUTF16(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)onString.value.
🎓 Cheat Sheet: What to Know for Each Level
🟢 Junior
Stringis immutable — its character contents cannot change after creation.- Literals reference the String Pool;
new String(...)allocates a distinct heap object. - Use
StringBuilderfor non-thread-safe modifications in loops;StringBufferis synchronized. ==checks reference identity;.equals()checks character content equality.- Use
+for simple 1-2 item concatenation; useStringBuilderinside 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 acoderflag (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_8when converting strings tobyte[].
🔴 Senior
- HotSpot
StringTable: Native C++ hash table, resizable since JDK 11 (JEP 341). substring()memory leak fix: Java 6 shared the backingchar[]; Java 7+ creates a defensive copy.invokedynamicconcatenation (JEP 280):StringConcatFactory.makeConcatWithConstantsoptimizes bytecode at runtime.@Stableannotation,coderfield, and SIMD vector instructions for hardware-acceleratedequals()and hashing.- Reflection attacks on
String.valueblocked by Strong Encapsulation (Java 16+ JEP 396). - G1 GC String Deduplication (
-XX:+UseStringDeduplication): Reclaims duplicatebyte[]without altering object headers.