Detailed explanation
A text block is a multi-line string literal that became a standard Java feature in Java 15, after preview versions in Java 13 and 14. It uses three double quotes (""") as opening and closing delimiters. Text blocks make embedded JSON, SQL, HTML, and other multi-line content easier to read because you usually do not need explicit newline escapes, repeated concatenation, or escaped double quotes. The result is still an ordinary java.lang.String, not a separate type or a template object.
The opening delimiter must be followed by a line terminator, optionally preceded by spaces or tabs; content cannot start immediately after the opening triple quotes. For example: String json = """ { "name": "Alice" } """; System.out.println(json); Here, the newline after the opening delimiter is not part of the value. The newline before the closing delimiter is included, so this string ends with a newline. Putting the closing delimiter immediately after the final closing brace would omit that trailing newline. Because println adds its own line separator, printing the first version also produces an extra blank line.
During compilation, Java normalises source line endings to LF, removes incidental indentation, and then interprets escape sequences. Incidental indentation is the common leading whitespace that allows the block to align with surrounding code; meaningful relative indentation is preserved. The closing delimiter's indentation also participates in determining how much leading whitespace is removed when it appears on its own line. In the example, the outer indentation disappears while the two spaces before the JSON property remain. Trailing whitespace on content lines is stripped during processing.
Text blocks are not raw strings: escapes such as \n, \t, and \\ still work. The \s escape preserves a space, including at a line end, while a backslash immediately followed by a line terminator suppresses that newline. Ordinary double quotes usually need no escaping. Text blocks provide no automatic variable interpolation; use formatting or concatenation when appropriate, and use prepared-statement parameters for SQL values. Interviewers typically expect correct delimiter syntax, awareness of indentation and trailing-newline behaviour, and recognition that the feature primarily improves source readability.
Key points to remember
- Text blocks became a standard feature in Java 15 and evaluate to ordinary String values.
- Use triple-double-quote delimiters; the opening delimiter requires a following line terminator.
- Multi-line content usually needs neither explicit \n escapes nor string concatenation.
- Java normalises line endings and strips incidental indentation while preserving relative indentation.
- Closing-delimiter placement affects the trailing newline; escape sequences still apply.
Common mistakes candidates make
- Putting content on the same line as the opening triple quotes, which causes a compilation error.
- Assuming every source space and newline is preserved exactly, ignoring indentation stripping and delimiter placement.
- Treating text blocks as raw strings or assuming they automatically substitute variables.
Likely follow-up questions
How can you avoid a trailing newline in a text block?
Place the closing triple quotes immediately after the final content character rather than on the next line. You can also use a backslash followed by a line terminator to suppress a content newline.
Can a text block contain variables?
It can contain placeholder text, but Java does not automatically interpolate variables inside a text block. For example, use a %s placeholder and call formatted(name) on the resulting String.
How do you preserve trailing spaces in a text block?
Use the \s escape for a space that must survive trailing-whitespace removal. Escape processing happens after whitespace stripping, so the space produced by \s is retained.
Interview tip: Show a small JSON text block, then explain the required opening newline, indentation stripping, and how closing-delimiter placement controls the final newline.
