JavaText Blocks

Text Blocks

Text blocks, standardized in Java 15, are a multi-line string literal syntax that starts and ends with three double quotes ("""). They let you write multi-line text — embedded JSON, SQL, HTML, or any other chunk of formatted text — directly in your source code, without a wall of \n escapes and + concatenation.

Before text blocks

Embedding multi-line content in a traditional string literal means every line break becomes an explicit \n, every line becomes its own quoted fragment, and the whole thing is stitched together with +. It works, but it’s noisy and easy to get wrong.

Before: concatenation and escaped newlines

Java
String json = "{\n" +
              "  \"name\": \"Alice\",\n" +
              "  \"age\": 30\n" +
              "}";

String html = "<html>\n" +
              "  <body>\n" +
              "    <p>Hello, World!</p>\n" +
              "  </body>\n" +
              "</html>";
After text blocks

The same content, written as a text block, reads exactly like the text it represents — no \n, no +, no escaped inner quotes.

After: a text block

Java
String json = """
    {
      "name": "Alice",
      "age": 30
    }
    """;

String html = """
    <html>
      <body>
        <p>Hello, World!</p>
      </body>
    </html>
    """;

Notice the inner double quotes in the JSON no longer need escaping — inside a text block, a single or double " is just a literal character, and only three-in-a-row (""") needs special handling.

Syntax rules
  • A text block opens with """ immediately followed by a line terminator — no content is allowed on the opening line itself.

  • It closes with a matching """, which can be on its own line or at the end of the last line of content.

  • The content between the delimiters becomes the string value, with incidental leading whitespace stripped automatically (see below).

  • A trailing line terminator is included unless the closing """ is placed right after the last character of content.

Incidental whitespace stripping
How indentation is determined
Text blocks let you indent their content to match your source code’s indentation without that indentation ending up in the string. The compiler looks at the *minimum* indentation shared by all non-blank lines (including the line with the closing `"""`) and strips exactly that much leading whitespace from every line. This means you can indent an entire text block to match the surrounding code, and the extra leading whitespace disappears from the actual string value — only whitespace *beyond* that common minimum survives as real content.

Indentation controlled by the closing delimiter

Java
String indented = """
        Hello
        World
        """;
// closing """ is indented 8 spaces, matching the content lines,
// so the resulting string is "Hello\nWorld\n" — no leading spaces

String withMargin = """
        Hello
        World
    """;
// closing """ is indented only 4 spaces — the common minimum is now 4,
// so 4 spaces of *real* leading whitespace remain on each content line
Escapes and formatting still work

Text blocks aren’t a separate string type — they produce an ordinary String, so every existing escape sequence (\n, \t, \\) still works inside one, and you can still use String.format() or the instance method formatted() to substitute values into the text.

Escapes and formatted() inside a text block

Java
String template = """
    Hello, %s!
    You have %d new messages.
    """;

String message = template.formatted("Alice", 3);
System.out.println(message);
// Hello, Alice!
// You have 3 new messages.

// \s at the end of a line preserves a trailing space that would
// otherwise be stripped, and \ at end-of-line suppresses the line break
String noBreak = """
    This is one \
    continuous line.
    """;
Great for SQL and JSON literals in tests
Text blocks shine anywhere you currently embed another language as a Java string — SQL queries, JSON/YAML fixtures in tests, HTML fragments, or shell scripts. The content stays readable and diff-friendly because it looks like the real thing, not an escaped one-liner.
  • Text blocks (Java 15+) use """ delimiters for multi-line string literals, replacing \n + concatenation.

  • Incidental leading whitespace is stripped based on the minimum indentation across all lines, including the closing delimiter.

  • A text block still produces a plain String — normal escapes, String.format(), and formatted() all still work.

  • \s preserves an otherwise-stripped trailing space, and a trailing \ suppresses a line break.