Mastery
Mastery/Java/G. Modern language features (8 → 21)
T1 · high-leverage

Text blocks (""", Java 15+)

A text block is a multi-line string literal with automatic incidental whitespace stripping: the common leading whitespace shared by every line (determined by the closing """'s indentation) gets removed, so you can indent the literal to match your code without that indentation ending up in the string. The result is character-for-character identical to the equivalent manually-escaped, concatenated string -- verified below, not just asserted.

java
String html = """
        <html>
            <body>
                <p>Hello, world</p>
            </body>
        </html>
        """;
System.out.println(html);
java
String oldWay = "<html>\n" +
        "    <body>\n" +
        "        <p>Hello, world</p>\n" +
        "    </body>\n" +
        "</html>\n";
System.out.println("identical content to the text block above: " + html.equals(oldWay));

A trailing backslash suppresses the line break (continues the same line), and \""" escapes a literal triple-quote without ending the block:

java
String withEscapes = """
        line one \
        still line one (backslash suppresses the newline)
        literal quote: \"""
        """;
System.out.println(withEscapes);

Text blocks compose naturally with String.formatted(...), which reads much better than concatenating a multi-line template piece by piece:

java
String withValues = """
        Name: %s
        Age: %d""".formatted("Ada", 36);
System.out.println(withValues);

Interview angle

Not a trick question so much as a "have you touched Java in the last few years" check — text blocks are Java 15+, and knowing the incidental-whitespace-stripping rule (rather than just "it's a multi-line string") shows real hands-on use rather than a skim of the release notes.

In the industry

Text blocks rapidly became the default for embedded SQL, JSON, HTML, and any other multi-line template string in Java codebases targeting 15+ — replacing the well-known eyesore of "line one\n" + "line two\n" + ... concatenation chains. The main adoption friction is purely version-based: codebases still on Java 8/11 LTS can't use them at all, which is itself a recurring argument for upgrading in teams that write a lot of embedded query or template strings.