Text Blocks in Java

Naveen AnamaneniUpdated · 4 min read

Writing a letter, a block of JSON or an SQL query as a normal string means a mess of "..." + "\n" + on every line, and escaped quotes everywhere. A text block (standard since Java 15) lets you write multi-line text the way it looks: between triple quotes, with real line breaks and plain " characters. Java even removes the indentation you used to line it up with your code.

A Letter as a Text Block

Letter.java▶ Try it
public class Letter {
    public static void main(String[] args) {
        String letter = """
            Dear Advitha,
              Your order #301 has shipped.
            Thanks,
            "PoolString Store"
            """;
        System.out.print(letter);
    }
}
Output
Dear Advitha,
  Your order #301 has shipped.
Thanks,
"PoolString Store"

How it works

  1. A text block starts with """ followed by a new line, and ends with """.
  2. Line breaks in the source become real line breaks (\n) in the string.
  3. Quotes inside need no escaping: "PoolString Store" is written as is.
  4. The extra spaces used to indent the block inside the method are removed, but the deliberate 2-space indent on the second line is kept.

How the Indentation Is Removed

The least-indented line, including the closing quotes, decides what's removed

Java looks at all the lines, including the line with the closing """, finds the smallest indentation, and removes exactly that much from every line. So the position of the closing quotes controls the result:

Closing """ placed...Effect
Lined up with the textNo leading spaces remain
Further left than the textEvery line keeps the difference as leading spaces
At the end of the last text line (Thanks""")No newline after the last line

Escapes You Still Have

EscapeMeaning
\ at the end of a lineJoin with the next line (no line break)
\sA single space that won't be stripped from the end of a line
\"""Three quotes inside the block
\n, \tStill work if you need them
Join.java▶ Try it
public class Join {
    public static void main(String[] args) {
        String sql = """
            SELECT name, marks FROM students \
            WHERE marks > 90""";
        System.out.println(sql);
 
        String json = """
            {"name": "%s", "marks": %d}""".formatted("Navika", 91);
        System.out.println(json);
    }
}
Output
SELECT name, marks FROM students WHERE marks > 90
{"name": "Navika", "marks": 91}

The backslash joins the two SQL lines into one, and formatted fills placeholders, which is handy for templates.

Common Mistakes

Starting the text on the same line as the opening """. That's a compile error; the opening quotes must be followed by a line break.

Being surprised by a trailing newline. If the closing """ is on its own line, the string ends with \n; use print instead of println, or put the quotes at the end of the last line.

Building SQL with formatted and user input. That invites SQL injection; database lessons later use parameters instead.

Practice

  1. Easy: Write your address as a text block of three lines and print it.
  2. Harder: Create an HTML snippet as a text block with a %s placeholder for a name, fill it with formatted("Srihitha"), and print it. Then move the closing """ four spaces left and observe the change.

Quick check: Text blocks

4 questions
1How does a text block start?
2Do you need to escape " inside a text block?
3What does a \ at the end of a line do?
4What decides how much indentation is removed?

Key Takeaways

Was this tutorial helpful?

Lesson 204 of 482