Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Text, Formatting and Regex

Java text blocks in depth

Deep dive into Java text blocks: how incidental whitespace is removed, the closing delimiter rule, line continuation, \s, quotes, stripIndent and formatted.

A text block is a multi-line string literal that starts and ends with three double quotes ("""). It was added as a standard feature in Java 15 so that JSON, SQL, HTML and other multi-line text can be written the way it will look, without \n and + on every line.

The compiler processes a text block in a fixed order: line endings are normalized to \n, incidental whitespace (indentation caused by your source layout) and trailing spaces are stripped, and only then are escape sequences interpreted. Knowing that order explains almost every surprise. For a gentle introduction see the keyword page text blocks; this page covers the rules in detail.

Java
String json = """
    {
      "name": "Ann"
    }
    """;

Try it

Java Where does the indentation go?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Escapes, continuation and formatted()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The rules

Opening delimiter. """ must be followed by optional spaces and a line break. The content starts on the next line. Text on the same line as the opening quotes is a compile error.

Incidental whitespace. The compiler looks at every non-blank line and at the line holding the closing delimiter (when the delimiter is alone on its line). The smallest indentation among those lines is removed from all of them. This is why moving the closing """ to the left keeps indentation in the result, while moving it to the right changes nothing.

Trailing whitespace. Spaces and tabs at the end of each line are removed. To keep a trailing space, end the line with \s (or \040).

Final newline. If the closing delimiter is on its own line, the string ends with \n. If it sits right after the last character, there is no trailing line feed.

Escapes. All ordinary escape sequences work (see escape sequences). Two exist only for text blocks: \<line terminator> removes the line break and \s is a space. A literal """ inside the block must have at least one quote escaped, as in \""".

Tabs and spaces. Whitespace is counted by characters, not by columns. Mixing tabs and spaces in the indentation gives misleading results; use one kind.

Syntax

Java
String s = """
    text
    """;
Related method Description
String.stripIndent() applies the incidental-whitespace rule to any string (Java 15)
String.translateEscapes() interprets escape sequences like the compiler would (Java 15)
String.formatted(Object...) String.format(this, args) (Java 15)
String.strip(), lines(), indent(int) handy for post-processing multi-line text

Usage notes

  • Portable line endings. Text blocks always use \n, even if the source file has Windows line endings, so the value is the same everywhere.
  • Not raw strings. Backslashes are still escapes: write \\d for a regex \d (a lone \d is a compile error). This is different from Python or Kotlin raw strings.
  • Constant. A text block without embedded expressions is a constant expression; it is interned like other literals and can be used in switch case labels.
  • No interpolation. Java has no ${} in text blocks. Use formatted() or String.format. String templates (STR."...") were a preview feature in Java 21 and 22 and were withdrawn in Java 23, so do not rely on them.
  • Format-friendly editors. Re-indenting code changes the closing delimiter's column; tools that reformat can silently alter the resulting string if the delimiter is not alone on its line.
  • Whitespace-only lines. Because trailing spaces are removed, a line holding only spaces becomes an empty line and is ignored when the indentation is measured.