Text blocks for multiline strings
Write multiline strings naturally with triple-quote text blocks.
Code Comparison
✕ Java 8
String json = "{\n" +
" \"name\": \"Duke\",\n" +
" \"age\": 30\n" +
"}";
✓ Java 15+
String json = """
{
"name": "Duke",
"age": 30
}
""";
Why the modern way wins
Readable as-is
JSON, SQL, and HTML look like real JSON, SQL, and HTML in your source.
No escape hell
Embedded quotes don't need backslash escaping.
Smart indentation
Leading whitespace is trimmed automatically based on the closing delimiter position.
Old Approach
String Concatenation
Modern Approach
Text Blocks
Since JDK
15
Difficulty
beginner
JDK Support
Text blocks for multiline strings
Available
Widely available since JDK 15 (Sept 2020)
How it works
Text blocks let you write multiline strings exactly as they appear. No more escaping quotes or adding \n. The compiler strips incidental indentation automatically.