🔤 Strings & Text · Beginner

Text blocks in Java

Triple-quoted multi-line strings and incidental indentation.

🧩 The mysteryFive lines of JSON used to look like "{\n \"name\": \"Ada\"\n}" in Java — a jungle of backslashes. Java 15 cleared the jungle with three quote marks.

Triple quotes

A text block (Java 15) is a multi-line String between """ delimiters. Line breaks are real line breaks and quotes need no escaping. What you see is (almost) what you get.

String json = """
    {
      "name": "Ada"
    }
    """;

Rule 1: open, then newline

The opening """ must be followed by a line break — the content starts on the next line. So a one-liner like """Hello""" is a compile error. For one line, use a normal "..." literal.

Incidental indentation

You indent text blocks to match your code — but that indentation isn't meant to be in the String. The compiler finds the smallest indentation among the content lines **and the closing """ line**, and strips it from every line. Relative indentation is kept.

🔮 Predict it

What survives?

What does this print?

String s = """
    Roses
      are red
    """;
System.out.print(s);
  1. Roses are red
  2. Roses are red
  3. Roses are red
Show the answer

The common 4 spaces (shared by the content and the closing """) are stripped. are red keeps its 2 extra spaces, because that indentation is relative.

🔮 Predict it

Where the closing quotes sit

Same text, different closing positions. What prints?

String a = """
    yes
    """;
String b = """
    yes""";
System.out.println(a.length());
System.out.println(b.length());
  1. 3 3
  2. 4 3
  3. 4 4
Show the answer

4 and 3. When the closing """ is on its own line, the text ends with a newline: a is "yes\n". In b the String closes right after yes, so there's no final newline.

⚠️ The trap

Moving the closing delimiter

The closing """ counts toward the common indentation. Put it at column 0 and the smallest indentation becomes 0 — nothing is stripped, and every line keeps its leading spaces. That's also the trick to keep indentation on purpose.

String s = """
    A
    B
""";
// "    A\n    B\n"

Two handy escapes

End a line with \ and it joins the next line — no newline inserted (great for long sentences). \s keeps a space that would otherwise be trimmed as trailing whitespace. Both arrived with text blocks in Java 15.

String s = """
    one long \
    line""";
// "one long line"
💼 In the real world

In real projects

Text blocks shine for SQL queries, JSON test fixtures, HTML snippets and expected output in unit tests. Code reviewers can finally read the embedded text instead of decoding escape sequences.

Key takeaways

  1. Opening """ must end its line
  2. Common leading spaces are stripped, including the closing line's
  3. Closing """ on its own line adds a final \n
  4. Quotes do not need escaping; \ at a line end joins lines
🤯 Did you know?

Text blocks previewed in Java 13 and 14 before going final in Java 15. An earlier design — raw string literals with backticks (JEP 326) — was withdrawn before it ever shipped.

Practice questions

What does this print?

String s = """
    Hi
      there
    """;
System.out.print(s);
  1. Hi there
  2. Hi there
  3. Hi there
  4. Hi there
Check your answer

Hi there. The smallest indentation (4 spaces, shared by the content and the closing delimiter) is removed. "there" keeps its 2 extra spaces.

What does this print?

String s = """
    A
    B
""";
System.out.print(s);
  1. A B
  2. A B
  3. A B
  4. A B
Check your answer

A B. The closing delimiter is at column 0, so the common indentation is 0 and nothing is stripped. Moving the closing """ is how you keep indentation.

Next: why does "hi😀".length() say 4, when you can count only 3 symbols?