skip to content

Strings

Everything about String: immutability and the literal pool, comparison, building strings efficiently, the common API, text blocks, and compact-string storage. Strings are the single most-asked non-collection Java class in interviews.

part ofJavaoverview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

Given the UTF-16-based String API, what subtle bugs arise from char-based operations on text with supplementary characters, and how do you handle them correctly?

level: seniorimportance: should knowfreq 35%

basics

~20 s

A Java char is one UTF-16 unit, but some characters (like many emoji) need two chars (a surrogate pair). So length() and charAt() count units, not real characters, and slicing or counting by char can split a character in half. Use code-point methods or codePoints() for correctness.

open as a page

What are the memory implications of String's internal representation, including object overhead and the savings from compact strings?

level: seniorimportance: should knowfreq 42%

basics

~20 s

A String costs more than just its text: the String object header, fields, and the separate backing array each have memory overhead. Compact strings cut the text bytes roughly in half for ASCII, but very short strings are dominated by fixed per-object overhead.

open as a page

Which string expressions get pooled at compile time, and why does "a"+"b" == "ab" but s+"b" (with s a variable) does not?

level: seniorimportance: should knowfreq 48%

basics

~20 s

The compiler folds expressions made only of constants (literals and final constants) into a single literal, which gets pooled. So "a"+"b" becomes "ab" at compile time and equals the pooled "ab". But if a variable is involved, the join happens at runtime and produces a fresh, un-pooled object.

open as a page

Are text blocks fully interchangeable with regular String literals, and what subtle pitfalls exist?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Yes — a text block compiles to an ordinary String, so it can go anywhere a String literal can, including switch labels and annotations. Pitfalls are mostly about hidden whitespace, the trailing newline, and the required newline after the opening delimiter.

open as a page

A teammate proposes using StringBuffer as a shared field so several worker threads can append log lines to it concurrently. Critique this design.

level: principalimportance: should knowfreq 40%

basics

~20 s

StringBuffer keeps each append from corrupting the buffer, but it does not control the order, makes every thread fight over one lock, and you still need extra synchronization to read it safely. A shared mutable builder is a bottleneck and a smell; give each thread its own builder or use a proper concurrent structure.

open as a page

As an API designer, how do you reason about the trade-offs of String immutability, and when does it become a liability (e.g. sensitive data)?

level: principalimportance: nice to knowfreq 34%

basics

~20 s

Immutability makes Strings safe and easy to share, but it has a downside: you cannot wipe a String from memory after use. For passwords and secrets, prefer char[] (or a byte buffer) that you can overwrite, because immutable Strings linger in memory and the pool.

open as a page

Why was String's internal representation able to change from char[] to byte[] without breaking applications, and what does that teach about API/implementation boundaries at platform scale?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

The internal array was private, and the public methods (length, charAt, etc.) kept their exact behavior. So the JVM could swap char[] for byte[] without changing anything visible to programs - well-behaved code never depended on the hidden field.

open as a page

When is calling intern() a good idea at scale, and what are the risks and alternatives?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

intern() helps when you have many duplicate runtime strings drawn from a small set, because it collapses them to one shared object and saves memory. It hurts when values are mostly unique, because the pool grows, lookups cost time, and GC suffers. Often a plain HashMap cache or JVM string deduplication is safer.

open as a page

showing 31–38 of 38