Text blocks in Java
Triple-quoted multi-line strings and incidental indentation.
"{\n \"name\": \"Ada\"\n}" in Java — a jungle of backslashes. Java 15 cleared the jungle with three quote marks.Triple quotes
A text block (Java 15) is a multi-line String between """ delimiters. Line breaks are real line breaks and quotes need no escaping. What you see is (almost) what you get.
String json = """
{
"name": "Ada"
}
""";Rule 1: open, then newline
The opening """ must be followed by a line break — the content starts on the next line. So a one-liner like """Hello""" is a compile error. For one line, use a normal "..." literal.
Incidental indentation
You indent text blocks to match your code — but that indentation isn't meant to be in the String. The compiler finds the smallest indentation among the content lines **and the closing """ line**, and strips it from every line. Relative indentation is kept.
What survives?
What does this print?
String s = """
Roses
are red
""";
System.out.print(s);Roses are redRoses are redRoses are red
Show the answer
The common 4 spaces (shared by the content and the closing """) are stripped. are red keeps its 2 extra spaces, because that indentation is relative.
Where the closing quotes sit
Same text, different closing positions. What prints?
String a = """
yes
""";
String b = """
yes""";
System.out.println(a.length());
System.out.println(b.length());3 34 34 4
Show the answer
4 and 3. When the closing """ is on its own line, the text ends with a newline: a is "yes\n". In b the String closes right after yes, so there's no final newline.
Moving the closing delimiter
The closing """ counts toward the common indentation. Put it at column 0 and the smallest indentation becomes 0 — nothing is stripped, and every line keeps its leading spaces. That's also the trick to keep indentation on purpose.
String s = """
A
B
""";
// " A\n B\n"Two handy escapes
End a line with \ and it joins the next line — no newline inserted (great for long sentences). \s keeps a space that would otherwise be trimmed as trailing whitespace. Both arrived with text blocks in Java 15.
String s = """
one long \
line""";
// "one long line"In real projects
Text blocks shine for SQL queries, JSON test fixtures, HTML snippets and expected output in unit tests. Code reviewers can finally read the embedded text instead of decoding escape sequences.
Key takeaways
- Opening """ must end its line
- Common leading spaces are stripped, including the closing line's
- Closing """ on its own line adds a final \n
- Quotes do not need escaping; \ at a line end joins lines
Text blocks previewed in Java 13 and 14 before going final in Java 15. An earlier design — raw string literals with backticks (JEP 326) — was withdrawn before it ever shipped.
Practice questions
What does this print?
String s = """
Hi
there
""";
System.out.print(s);- Hi there
- Hi there
- Hi there
- Hi there
Check your answer
Hi there. The smallest indentation (4 spaces, shared by the content and the closing delimiter) is removed. "there" keeps its 2 extra spaces.
What does this print?
String s = """
A
B
""";
System.out.print(s);- A B
- A B
- A B
- A B
Check your answer
A B. The closing delimiter is at column 0, so the common indentation is 0 and nothing is stripped. Moving the closing """ is how you keep indentation.