All topics
library
beginner

Text Blocks (Java 15)

Write multi-line string literals with natural formatting using triple-quote syntax.

Triple quotes give you a multi-line string with no escapes.

Text blocks = pasting a note directly into your code, exactly as it looks. Traditional strings = retyping the note with escape codes for every newline and quote.

Key Concepts

1
    String query = """
        SELECT id, name
        FROM users
        WHERE status = 'active'
        """;
2
Compare the old form:
    String query = "SELECT id, name\n" +
                   "FROM users\n" +
                   "WHERE status = 'active'\n";
3
The opening """ must be followed by a newline. Content starts on the next line. Putting text straight after the quotes is a compile error.
"""
4
Indentation is stripped intelligently. The compiler finds the smallest indentation across all content lines and the closing delimiter, then removes that much from every line:
5
    String s = """
        one
          two
        """;
    // "one\n  two\n"  -- the common 4 spaces went, the extra 2 stayed
6
So the closing delimiter controls the baseline. Move it left and every line keeps more indentation; move it right and more is stripped. That is the knob people miss.
7
Trailing spaces are removed, which protects you from invisible whitespace. Use \s to keep one deliberately:
\s
8
    "value \s"
9
A trailing backslash joins lines without inserting a newline:
10
    String s = """
        a very long line that continues \
        on the next source line
        """;
11
Quotes need no escaping, which is the biggest win for JSON:
12
    String json = """
        {"name": "Asha", "role": "admin"}
        """;
13
Three consecutive quotes inside still need one escaped.
14
The result is an ordinary String. It is interned like any literal, and equals, formatted and concat all work normally. The processing is entirely at compile time, so there is no runtime cost.
Stringequalsformattedconcat
15
Where it earns its place: SQL, JSON, XML, HTML and regular expressions — anything you would otherwise paste and then hand-escape.
16
What the interviewer is probing.1. "What decides how much indentation is stripped?" Probing: the closing delimiter, which people miss. Stalls: "The first line." Moves up: the smallest indentation across all content lines and the closing delimiter — moving the closing quotes changes the result.
17
2. "Can you put text on the same line as the opening quotes?" Probing: the syntax rule. Stalls: "Yes." Moves up: no — the opening delimiter must be followed by a line terminator, or it does not compile.
18
3. "How do you keep a trailing space?" Probing: the escape that exists for it. Stalls: "It is kept." Moves up: trailing spaces are stripped; use the \s escape to protect one.
19
4. "Does a text block create a different kind of String?" Probing: whether processing is compile time or runtime. Stalls: "It is a special type." Moves up: no — it is an ordinary String, interned like any literal, with all processing done at compile time.