Text Blocks (Java 15+) — Free Java Tutorial

Learn Text Blocks (Java 15+) in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

Text Blocks (Java 15+) — Free Java Tutorial

Learn Text Blocks (Java 15+) in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

✓ 100% Free ✓ No Login Needed ✓ NCERT / CBSE Aligned ✓ Download as PDF

TL;DR: Learn Text Blocks (Java 15+) in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated

🤖 Stuck on any question? Ask Syllab's free AI Tutor for a step-by-step explanation — instant, unlimited, no login.

Text Blocks (Java 15+) in Java

Text blocks (Java 15+) are multi-line string literals surrounded by triple double-quotes """. They preserve formatting and eliminate the need for escape sequences.

The opening """ must be followed by a newline. Indentation is automatically stripped based on the least-indented line (incidental whitespace removal).

Text blocks support the same escape sequences as regular strings. Use \n for explicit newlines, \t for tabs. String.formatted() or formatted() works the same as String.format().

Text blocks are ideal for: SQL queries, JSON/XML literals, HTML templates, and any multi-line string content.

Text Blocks (Java 15+) — Syntax

// Old way
String html = "<html>
" +
              "  <body>Hello</body>
" +
              "</html>";

// Text block
String html = """
        <html>
          <body>Hello</body>
        </html>
        """;

// With formatting
String json = """
        { "name": "%s", "age": %d }
        """.formatted(name, age);

Learn Text Blocks (Java 15+) step by step with Syllab's free interactive Java tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Java course →