library
beginnerText Blocks (Java 15)
Write multi-line string literals with natural formatting using triple-quote syntax.
Text blocks (preview in Java 13, stable in Java 15) use triple-quote delimiters to write multi-line strings without escape sequences for newlines and quotes.
Text blocks = pasting a note directly into your code, exactly as it looks. Traditional strings = retyping the note with escape codes for every newline and quote.
Key Concepts
1
Syntax: the opening """ must be followed by a newline. Content starts on the next line. The closing """ determines the indentation baseline — any common leading whitespace is stripped.
2
Key features:
- Natural line breaks (no \n)
- Embedded quotes without escaping (no \")
- Automatic indentation management
- \s for trailing space preservation (otherwise trailing spaces are stripped)
- \ at end of line joins lines (no newline inserted)
3
Indentation rules: the compiler calculates the common leading whitespace across all content lines and the position of the closing """. It removes that many leading spaces from each line. Moving the closing """ left adds indentation; moving it right removes it.
4
Text blocks produce regular String objects — they're interned in the String Pool and work with equals(), concat, formatted(), etc.
5
Ideal for: SQL queries, JSON/XML templates, HTML snippets, regex patterns, and any multi-line content.