Explain Java String Storage and Construction Choices

Quick Overview

Explain Java string pooling, literals, explicit new String construction, interning, immutability, equality, and object identity. Compare compile-time and runtime concatenation, then choose appropriately among strings, builders, and related construction patterns.

Explain Java String Storage and Construction Choices

Company: Oracle

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

## Question Explain Java's string pool and distinguish a string literal, `new String(...)`, and `StringBuilder`. Show when each form is appropriate and what identity, immutability, and concatenation behavior a developer should expect. ### Constraints & Assumptions - Discuss ordinary modern Java semantics without relying on a particular garbage collector. - Distinguish value equality from object identity. - Include the effect of compile-time constant concatenation and runtime loop concatenation. - Avoid claiming that interning is always a memory optimization. ### Clarifying Questions to Ask - Should the answer cover `String.intern()` explicitly? Yes, including its trade-offs. - Is thread safety relevant? Contrast `StringBuilder` with immutable strings; mention `StringBuffer` only as context. - Do you need byte-level layout details? No, language-level behavior and practical performance are sufficient. ### Part 1 — Pooling and Construction Explain where literals obtain their canonical references, what `new String("x")` guarantees, and why `==` is not a content comparison. #### What This Part Should Cover - Literal reuse, explicit allocation, `intern()`, `equals`, and identity. ### Part 2 — Immutability and Building Text Explain why `String` is immutable, how concatenation is compiled, and why repeated runtime appends should normally use `StringBuilder`. #### What This Part Should Cover - Safety of sharing strings, compiler folding, builder mutation, and final conversion. ### Part 3 — Choosing the Type Give concrete examples for literals or stable values, explicit construction, and iterative assembly. #### What This Part Should Cover - A decision based on semantics first and allocation cost second. ```hint Separate reference reuse from value equality Two references may point to the same pooled object, but correct content comparisons still call `equals`. ``` ### What a Strong Answer Covers - Correct language-level claims without depending on accidental JVM behavior. - A practical performance explanation that does not overstate micro-optimizations. - Examples involving literals, runtime inputs, loops, and API boundaries. ### Follow-up Questions 1. What does the compiler typically do with `"a" + "b"`? 2. Why can indiscriminate interning increase memory pressure? 3. When would `StringBuffer` be considered instead of `StringBuilder`?

Overview: Explain Java string pooling, literals, explicit new String construction, interning, immutability, equality, and object identity. Compare compile-time and runtime concatenation, then choose appropriately among strings, builders, and related construction patterns.

|Home/Software Engineering Fundamentals/Oracle
Oracle logo
Oracle
Aug 21, 2026
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
1
0

Question

Explain Java's string pool and distinguish a string literal, new String(...), and StringBuilder. Show when each form is appropriate and what identity, immutability, and concatenation behavior a developer should expect.

Constraints & Assumptions

  • Discuss ordinary modern Java semantics without relying on a particular garbage collector.
  • Distinguish value equality from object identity.
  • Include the effect of compile-time constant concatenation and runtime loop concatenation.
  • Avoid claiming that interning is always a memory optimization.

Clarifying Questions to Ask Guidance

  • Should the answer cover String.intern() explicitly? Yes, including its trade-offs.
  • Is thread safety relevant? Contrast StringBuilder with immutable strings; mention StringBuffer only as context.
  • Do you need byte-level layout details? No, language-level behavior and practical performance are sufficient.

Part 1 — Pooling and Construction

Explain where literals obtain their canonical references, what new String("x") guarantees, and why == is not a content comparison.

What This Part Should Cover Guidance

  • Literal reuse, explicit allocation, intern() , equals , and identity.

Part 2 — Immutability and Building Text

Explain why String is immutable, how concatenation is compiled, and why repeated runtime appends should normally use StringBuilder.

What This Part Should Cover Guidance

  • Safety of sharing strings, compiler folding, builder mutation, and final conversion.

Part 3 — Choosing the Type

Give concrete examples for literals or stable values, explicit construction, and iterative assembly.

What This Part Should Cover Guidance

  • A decision based on semantics first and allocation cost second.

What a Strong Answer Covers Guidance

  • Correct language-level claims without depending on accidental JVM behavior.
  • A practical performance explanation that does not overstate micro-optimizations.
  • Examples involving literals, runtime inputs, loops, and API boundaries.

Follow-up Questions Guidance

  1. What does the compiler typically do with "a" + "b" ?
  2. Why can indiscriminate interning increase memory pressure?
  3. When would StringBuffer be considered instead of StringBuilder ?
Loading comments...