skip to content

Text Blocks

Triple-quoted text blocks preserve multi-line content while stripping incidental indentation, with \s and trailing backslash for fine control. Interviewers ask about the indentation rule because it is the part that surprises people.

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

questions

4

What is a text block in Java, and how do you declare one?

level: juniorimportance: must knowfreq 70%

answer

  1. Triple-quote delimiters """
  2. Java 15 standard (JEP 378), preview in 13/14
  3. Opening """ must be followed by a newline
  4. Still just a String at runtime
  5. Readability for JSON/SQL/HTML

basics

~10 s

A text block is a multi-line string literal that starts and ends with three double-quotes ("""). It lets you write text spanning several lines without escaping every newline or quote.

solid answer

~40 s

A text block, added as a standard feature in Java 15, is a multi-line string literal delimited by three double-quotes. The opening delimiter must be followed by a line terminator, and the content begins on the next line; the closing delimiter is another triple-quote. Its purpose is readability: you can write JSON, SQL, HTML, or any multi-line text with real newlines and unescaped embedded double-quotes, instead of concatenating strings with \n. A text block is still just a String at runtime, indistinguishable from any other String. The compiler processes its content at compile time, stripping incidental whitespace and translating escapes, so the result is a normal compile-time constant.

code

java · 8 lines
java
String json = """
        {
            "name": "Ada",
            "role": "engineer"
        }
        """;
// Equivalent to the escaped one-liner:
// String json = "{\n    \"name\": \"Ada\",\n    \"role\": \"engineer\"\n}\n";

go deeper

for a junior

Recognize the """ syntax, know it makes multi-line strings, and that it produces a normal String.

for a middle

Explain the required newline after the opening delimiter, the readability motivation, and that processing happens at compile time.

for a senior

Articulate that it is a compile-time constant identical to a regular String, name the Java 15 standardization, and connect it to the three-step content algorithm.

for a principal

Discuss when to prefer text blocks vs. external resource files or formatting libraries, migration/version considerations across a codebase, and the interning/constant-folding implications.

## What a text block is A **string literal** in Java is source text that the compiler turns into a `String` object, for example `"hello"`. A traditional string literal must fit on one line, and any double-quote inside it must be escaped (`\"`), as must any newline (`\n`). Building multi-line text this way is noisy and error-prone. A **text block** is a second kind of string literal, introduced as a *preview* in Java 13/14 and made a permanent, standard feature in **Java 15** (JEP 378). It is delimited not by a single double-quote but by a sequence of **three double-quote characters** (`"""`), called the *opening* and *closing delimiters*. ## The required shape ```java String s = """ Hello, world! """; ``` Rules of the shape: 1. The **opening delimiter** is `"""` followed by optional spaces/tabs and then a **line terminator** (a newline). You may NOT put content on the same line as the opening `"""` — `"""Hello` does not compile. The actual content starts on the next line. 2. The **content** is everything from that next line up to the closing delimiter. 3. The **closing delimiter** is another `"""`. It may sit on its own line or at the end of the last content line. ## Why it exists It is purely a *readability and authoring* feature. Inside a text block: - Newlines are written as actual line breaks, not `\n`. - A double-quote needs no escaping (`He said "hi"` is fine), because a single `"` no longer ends the literal — only three in a row do. Typical uses: embedding JSON, SQL queries, HTML, XML, or formatted multi-line messages. ## It is still a String A text block produces an ordinary `java.lang.String`. At runtime there is *no* `TextBlock` type — `"""..."""` and `"..."` that yield equal text produce `equal` (and even interned-identical, since both are compile-time constants) Strings. The text block's content is computed entirely at **compile time** through three steps applied in order: (1) line-terminator normalization, (2) incidental-whitespace stripping, (3) escape-sequence interpretation. Those steps are the subject of the other questions in this topic. ## A first-principles takeaway Think of a text block as "write the text as you want it to look, indent it to match your code, and the compiler figures out which whitespace was decoration and which was real."

  • Can you write content on the same line as the opening delimiter, like """hello?
    No. The opening """ must be followed only by optional whitespace and then a line terminator; the content begins on the next line. Putting text right after the opening triple-quote is a compile error.
  • At runtime, how does a text block differ from a normal string literal?
    It does not differ at all. Both produce a java.lang.String, computed at compile time, so two literals with identical resulting text are equal and interned to the same instance.

saying these in an interview costs you the question

  • Claiming text blocks are a distinct type from String
  • Saying you can put content on the same line as the opening triple-quote
  • Thinking text blocks are a runtime feature rather than compile-time processed
  • Believing it was added in Java 8 or 11

context

open as a page

How does the compiler strip incidental whitespace from a text block, and what determines the minimal indentation?

level: middleimportance: must knowfreq 68%

basics

~20 s

The compiler removes the common leading whitespace shared by every non-blank line and the closing delimiter line. So you can indent the block to match your code, and that indentation is dropped from the result.

open as a page

How do you control whether a text block ends with a newline, and how do the \s and trailing \ escapes work?

level: middleimportance: should knowfreq 58%

basics

~20 s

If the closing """ is on its own line, the result ends with a newline; put the closing """ right after the last character to drop it. The \s escape preserves a trailing space, and a trailing backslash joins a line to the next (no newline).

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