Text Blocks

A text block is a multi-line string literal — opened and closed with triple double-quotes — that lets you write JSON, SQL, and HTML naturally, without escaping every newline or gluing lines together with + .

Learn Text Blocks in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You should know how ordinary Strings and escape sequences like \n work, and ideally have seen var . Text blocks need Java 15 or newer .

💡 Analogy: Writing a multi-line string the old way is like reading a poem aloud and saying "new line" after every single verse — exhausting and easy to get wrong ( "a\n" + "b\n" + ... ). A text block is like handing someone the printed page: the line breaks are already there . You write the text once, exactly as it should look, between two """ fences.

And because the compiler strips the indentation you used to line the block up with your code, the block looks neatly indented in your source but produces a clean, un-indented string.

Open with """ followed by a newline (nothing else may sit on that line), write your content, then close with """ . Everything between is the string. The example below produces exactly the same String as the old concatenation-with- \n approach — proven with .equals .

The clever part: the compiler scans every content line and the closing """ , finds the smallest shared leading indentation, and strips exactly that much from each line. That common, removable part is incidental whitespace; what remains is essential . This is how a block indented to match your code still yields an un-indented string.

In the second example below, moving the closing """ to the far left lowers the shared minimum, so four leading spaces survive on each line.

Two escapes are unique to text blocks. A backslash at the end of a line removes that line's newline, so a long sentence can be wrapped across several source lines yet stay one line in the string. The \s escape is a single space that also blocks the compiler from trimming trailing spaces — useful when trailing spaces actually matter.

Java has no ${" name "} -style interpolation. To drop values into a block, write %s / %d placeholders and call .formatted(args...) on it — a tidy instance shortcut for String.format . This makes text blocks ideal for JSON payloads and SQL queries built from variables.

Predict the result before opening each answer.

Answer: No. Content may not start on the opening line — the """ must be followed by a line terminator. Put hi on the next line.

Answer: 2 — just ab . The four leading spaces are incidental (the closing """ sits at column 4 too) and there's no trailing newline because """ follows ab directly.

Answer: a is "done\n" (closing """ on its own line adds a trailing newline); b is "done" with no newline. Delimiter placement controls the trailing newline.

🎯 Your Turn — A formatted report

Produce the exact multi-line report using a single text block and .formatted(...) .

🧩 Mini-Challenge — Build an HTML list

Loop items into list rows, then wrap them in a <ul> using a text block.

You can now write multi-line strings cleanly with text blocks, you understand how incidental whitespace is stripped, you know the \ and \s escapes, and you can turn a block into a template with .formatted() .

Next up: Switch Expressions — a modern, fall-through-free switch that produces a value.

Practice quiz

How do you open a text block?

  • A single "
  • Three double-quotes followed by a line terminator
  • Backticks
  • Triple single-quotes

Answer: Three double-quotes followed by a line terminator. A text block opens with """ and a newline; content begins on the next line.

In which Java version did text blocks become a standard feature?

  • Java 8
  • Java 11
  • Java 15
  • Java 21

Answer: Java 15. Text blocks previewed in 13/14 and became standard in Java 15 (JEP 378).

What is 'incidental' whitespace?

  • Trailing spaces only
  • The common leading indentation the compiler strips
  • Tabs only
  • Whitespace inside strings

Answer: The common leading indentation the compiler strips. The compiler finds the minimum indentation across all lines (and the closing delimiter) and removes that much from every line.

What does a trailing backslash \ at the end of a line in a text block do?

  • Inserts a literal backslash
  • Suppresses the newline, joining the line to the next
  • Escapes a quote
  • Adds a space

Answer: Suppresses the newline, joining the line to the next. A line-ending \ removes the newline so two source lines become one logical line.

What does \s mean in a text block?

  • A tab
  • A newline
  • A single space that is not stripped
  • A null character

Answer: A single space that is not stripped. \s is an escaped space; it is exactly one space and prevents trailing-space stripping.

Where must the content of a text block start?

  • On the same line as the opening """
  • On the line after the opening """
  • Anywhere
  • At column 0

Answer: On the line after the opening """. Nothing except whitespace may follow the opening """ on its line; content starts on the next line.

Does the position of the closing """ affect indentation?

  • No
  • Yes — it participates in determining the common indentation to strip
  • Only in Java 21
  • Only with \s

Answer: Yes — it participates in determining the common indentation to strip. The closing delimiter's indentation is part of the whitespace calculation, so moving it left keeps more leading spaces.

How do you insert variable values into a text block?

  • Use ${var} interpolation
  • Text blocks cannot hold variables
  • Use .formatted(args) or String.format
  • Use + concatenation only

Answer: Use .formatted(args) or String.format. Java has no string interpolation; call .formatted(...) on the block (or String.format) to fill %s/%d placeholders.

Are you required to escape a single " inside a text block?

  • Yes, always
  • No — a lone double-quote is fine; only three in a row need care
  • Only at line start
  • Only in JSON

Answer: No — a lone double-quote is fine; only three in a row need care. Single and double quotes need no escaping inside a text block; you only manage sequences of three quotes.

What is the type of a text block expression?

A text block is just a String literal written across multiple lines — its type is String.