Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

Java text blocks

Reference for Java text blocks, the triple-quote multi-line string literals: delimiters, indentation stripping, escapes, line continuation and formatting.

A text block is a string literal that spans several lines and starts and ends with three double quotes ("""). It lets you write JSON, SQL, HTML, or any multi-line text exactly as it should look, without \n escapes and without escaping every double quote. Text blocks became a standard feature in Java 15.

The result is an ordinary String. The compiler removes the indentation you only added to make the source tidy, so the text is not shifted to the right.

Java
String json = """
        {
          "name": "Ada",
          "year": 1843
        }
        """;

Try it

Java Indentation and quotes

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 and line continuation

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.

Syntax

Java
"""<optional spaces><line terminator>
    content lines
    closing"""

The opening """ must be followed by a line break (only spaces, tabs, or form feeds may sit between them). The content begins on the next line.

Rule Description
Incidental indentation The smallest indentation among all non-blank lines and the closing delimiter line is removed from every line.
Trailing spaces Removed from each line, unless protected by \s or an escape.
Line endings Normalized to \n regardless of the file's line endings.
Last newline Present when the closing """ is on its own line; absent when it follows text on the same line.
\<newline> Line continuation: the line break is not part of the string.
\s A single space (survives the stripping of trailing spaces).
Other escapes \n, \t, \", \\, \uXXXX and the rest work as in normal strings.

Usage notes

  • The closing delimiter controls the indentation. The compiler measures the closing line's indentation along with the text lines. If the delimiter is indented less than the text, the difference stays in the string (the last example above keeps four spaces before body); if it sits on the same line as the last text, only the text lines count.
  • Double quotes need no escaping, apart from three in a row, where you escape at least one (\""").
  • Mixing tabs and spaces is hazardous. Whitespace is counted by characters, not by display width, so a line indented with a tab has smaller indentation than one with four spaces. Stick to one in a block.
  • Interpolation does not exist (there is no $"{x}"). Use String.formatted(...), String.format(...), or MessageFormat. String templates were previewed in Java 21 and 22 but were removed in Java 23, so do not rely on them.
  • Constant folding: a text block without placeholders is a compile-time constant and is interned like any other literal. Concatenating it with other constants is also constant.
  • Helpful methods: lines(), stripIndent(), translateEscapes() (Java 15) work on any string and are what the compiler uses internally.
  • Language injection: IDEs can recognize embedded SQL or JSON in a text block and check it.
  • SQL with user input: never build SQL by pasting values into a text block; use prepared statements.
  • Not for resource files: large texts belong in resource files or databases, not in source code.