Home › coding › java › java text blocks

Java Text Blocks — Free Java Tutorial

Learn Java Text Blocks 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 Java Text Blocks 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 Jul 14, 2026

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

Java Text Blocks 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.

Java Text Blocks — Syntax

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

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

// With formatting
String json = """
        { "name": "%s", "age": %d }
        """.fo

Learn Java Text Blocks 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 →

Explore:

  • Syllabus
  • Practice
  • Mock Tests
  • NCERT Solutions
  • Coding
  • GK Quiz
  • Career Predictor
  • AI Tutor
  • Live Quiz
  • Doubt Solver
  • Microlearning
  • Free Alternatives
  • Kids Zone
  • Study Room
  • Calculators
  • Worksheets

Syllab.in — Free learning for Indian students, Class 1–12