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
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
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
Indentation controlled by the closing delimiter
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 lineEscapes 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
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.
""";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(), andformatted()all still work.\spreserves an otherwise-stripped trailing space, and a trailing\suppresses a line break.