Modern Java Evolution
Text Blocks
Text blocks (Java 15) hold multi-line strings between
""" delimiters: no escape ladders, automatic incidental-indentation stripping, and readable embedded JSON, SQL, and HTML.- Open with
"""+ newline; close alignment controls indentation stripping - The closing delimiter's column sets the left margin — move it to control indentation
\at line end joins lines (no newline);\skeeps trailing spaces- Quotes inside need no escaping;
\n,\tstill work if wanted - It's still a
String— same type, same methods, interning included
String jsonOld = "{\n" +
" \"name\": \"Duke\",\n" +
" \"role\": \"mascot\"\n" +
"}";
String json = """
{
"name": "Duke",
"role": "mascot"
}
"""; // closing delimiter position strips the common indentThe indentation algorithm: the compiler finds the minimal indentation across all lines including the closing delimiter line, and strips it — so out-denting the closing """ preserves visual nesting in source while producing flush-left output. String.stripIndent(), translateEscapes(), and formatted() are the companion methods; """…%s…""".formatted(value) covers interpolation until/unless a dedicated feature lands.