What is a text block in Java, and how do you declare one?
answer
- Triple-quote delimiters """
- Java 15 standard (JEP 378), preview in 13/14
- Opening """ must be followed by a newline
- Still just a String at runtime
- Readability for JSON/SQL/HTML
basics
~10 sA 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 sA 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 linesString json = """
{
"name": "Ada",
"role": "engineer"
}
""";
// Equivalent to the escaped one-liner:
// String json = "{\n \"name\": \"Ada\",\n \"role\": \"engineer\"\n}\n";go deeper
Recognize the """ syntax, know it makes multi-line strings, and that it produces a normal String.
Explain the required newline after the opening delimiter, the readability motivation, and that processing happens at compile time.
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.
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