Text Blocks
BeginnerText blocks provide a clean way to write multiline strings — JSON, HTML, SQL — without escape sequences and string concatenation.
Overview
Text blocks, finalized in Java 15, are a multiline string literal delimited by triple quotes ("""). They automatically handle indentation stripping, making it easy to embed JSON, HTML, SQL, or any multiline content directly in Java code. Text blocks reduce noise from escape sequences (\n, \", \\) and string concatenation, improving readability significantly.
Basic Syntax
A text block opens with """ followed by a newline (the opening delimiter cannot be on the same line as content). The closing """ determines the indentation baseline — any leading spaces up to the column of the closing """ are stripped.
This means you can indent the text block with your code and the indentation is automatically removed from the content.
// Old way — messy escape sequences
String json = "{\n" +
" \"name\": \"Alice\",\n" +
" \"age\": 30\n" +
"}";
// Text block — clean and readable
String json = """
{
"name": "Alice",
"age": 30
}
""";
System.out.println(json);
// {
// "name": "Alice",
// "age": 30
// }Indentation and Trailing Newline
The position of the closing """ controls indentation stripping. If the closing """ is at column 8, 8 spaces are stripped from each line.
A text block always ends with a newline unless you place the closing """ on the last content line. String::stripIndent() and String::translateEscapes() are the underlying methods.
// Closing """ on its own line → trailing newline included
String withNewline = """
Hello
World
""";
// "Hello\nWorld\n"
// Closing """ on last content line → no trailing newline
String withoutNewline = """
Hello
World""";
// "Hello\nWorld"
// HTML example
String html = """
<html>
<body>
<p>Hello, %s!</p>
</body>
</html>
""".formatted("Alice");Escape Sequences and Line Continuations
Text blocks support a new escape: \<line-terminator> suppresses the newline at the end of a line, allowing you to split a long string across source lines without embedding a literal newline.
\s at the end of a line prevents trailing whitespace from being stripped — useful when the content requires it.
// Line continuation — no newline in the result
String longSql = """
SELECT id, name, email FROM users WHERE active = true ORDER BY name""";
// Equivalent to one long line
// s preserves trailing space
String tsv = """
col1 s
col2 s
""";Key Points to Remember
- Text blocks use triple quotes """; opening """ must be followed by a newline.
- Incidental indentation is automatically stripped based on closing """ position.
- Text blocks end with a newline unless closing """ is on the last content line.
- Use \<newline> to suppress a line break, and \s to preserve trailing whitespace.
- Text blocks are ordinary String objects at runtime — no new type.
Practice Text Blocks in the Playground
Run and modify code directly in your browser - no setup needed.
Interview Questions
Sign in to ask AriaHow does Java determine how much indentation to strip from a text block?
What is the purpose of the \s escape sequence in text blocks?
Can you use text blocks for SQL queries? What are the benefits?
What is the difference between a text block with closing """ on its own line vs. inline?
Are text blocks interned like regular string literals?
Ask Aria about Text Blocks
Your personal AI tutor — ask anything about this concept