Master Core Java Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Module 9

Text Blocks in Java

Learn how Java text blocks simplify multi-line strings and formatted text.

๐Ÿ“

1. What are Text Blocks?

A text block is a multi-line string literal introduced as a preview feature in Java 13 and standardized in Java 15.

Text blocks make it easier to write multi-line text such as:

  • HTML
  • JSON
  • SQL
  • XML
  • Formatted messages
  • Multi-line documentation
Key idea: Text blocks reduce the need for repeated quotation marks, newline escape sequences, and string concatenation.

2. Traditional Multi-Line String

Before text blocks, multi-line strings often required explicit newline characters and string concatenation.

Java
String message =
    "Hello Java\n"
    + "Welcome to CIIT Institute\n"
    + "Learn modern Java features";

3. Basic Text Block

A text block starts and ends with three double quotation marks.

Java
String message = """
        Hello Java
        Welcome to CIIT Institute
        Learn modern Java features
        """;

The text is much easier to read because the source code resembles the resulting multi-line string.

4. Text Block Syntax

The opening delimiter is normally followed by a line break. The closing delimiter ends the text block.

Java
String text = """
        First line
        Second line
        Third line
        """;

5. Newline Characters

Each line in a text block normally contributes a line terminator to the resulting string.

Java
String text = """
        Java
        Spring
        Hibernate
        """;

Conceptually, the resulting value contains:

Output
Java
Spring
Hibernate

6. Automatic Indentation

Java processes incidental indentation in text blocks so that the indentation used only to format source code does not unnecessarily become part of the resulting string.

Java
String text = """
        Java
        Spring
        SQL
        """;

This allows developers to indent the text block together with the surrounding Java code while keeping the resulting content clean.

7. Text Blocks with HTML

Text blocks are especially useful when embedding HTML.

Java
String html = """
        <html>
            <body>
                <h1>CIIT ๐ŸŒ๐Ÿ‘จโ€๐Ÿซ</h1>
            </body>
        </html>
        """;
Benefit: HTML structure remains readable without excessive escape sequences.

8. Text Blocks with JSON

JSON data can also be represented cleanly with text blocks.

Java
String json = """
        {
            "name": "Pradnya",
            "role": "Java Developer",
            "experience": "Fresher"
        }
        """;

9. Text Blocks with SQL

SQL queries become easier to format and maintain.

Java
String sql = """
        SELECT id, name, email
        FROM users
        WHERE active = true
        ORDER BY name
        """;

10. Escape Sequences

Text blocks still support Java escape sequences when they are required.

Java
String text = """
        First line
        Second line
        """;

Common escape sequences include:

  • \n - New line
  • \t - Tab
  • \" - Double quote
  • \\ - Backslash

11. Double Quotes Inside Text Blocks

Double quotes can usually be written directly inside a text block without escaping each quote.

Java
String message = """
        He said "Hello Sam"
        and started learning.
        """;

12. Text Blocks are Strings

A text block produces a normal Java String. Therefore, normal String methods can be used.

Java
String text = """
        Java
        Spring
        """;

System.out.println(
    text.length()
);

System.out.println(
    text.contains("CIIT๐ŸŒโค๏ธ")
);

System.out.println(
    text.toUpperCase()
);

13. Formatted Text Blocks

Text blocks can be combined with String formatting methods.

Java
String name = "Samadhan";
int age = 22;

String profile = """
        Name: %s
        Age: %d
        """.formatted(name, age);

System.out.println(profile);
Modern Java: The formatted() method provides a convenient way to insert values into a text block.

14. String.format with Text Blocks

You can also use String.format() with a text block.

Java
String name = "CIIT Institute";

String message = String.format(
    """
    Learning language: %s
    """,
    name
);

System.out.println(message);

15. Incidental Whitespace

Text blocks distinguish between indentation introduced only by source formatting and whitespace that is intentionally part of the content.

Java
String text = """
        Java
          Spring
            Boot
        """;

Relative indentation inside the text can be preserved when it represents meaningful content.

16. Trailing Spaces

Trailing whitespace can be controlled explicitly when necessary. Java provides the \s escape sequence for preserving trailing spaces at the end of a line.

Java
String text = """
        Java\s
        Spring
        """;

17. Line Continuation

The backslash escape can be used to prevent a source line break from becoming a line break in the resulting string.

Java
String text = """
        Java \
        Spring \
        Boot
        """;

This can be useful when source formatting should not introduce additional newline characters.

18. Converting Existing Strings

Text blocks are most useful when existing concatenated strings become difficult to read or maintain.

Before
String html =
    "<html>\n"
    + "    <body>\n"
    + "        <h1>WelCome CIIT Training Institute๐ŸŒ๐Ÿ‘€</h1>\n"
    + "    </body>\n"
    + "</html>";
After
String html = """
        <html>
            <body>
                <h1>WelCome CIIT Training Institute๐ŸŒ๐Ÿ‘€</h1>
            </body>
        </html>
        """;

19. Java Version

Java Version Text Blocks
Java 13 Introduced as preview
Java 14 Second preview
Java 15 Standard feature

20. Advantages of Text Blocks

  • Improves readability of multi-line strings.
  • Reduces repeated string concatenation.
  • Reduces unnecessary newline escape sequences.
  • Makes HTML and JSON easier to maintain.
  • Makes SQL queries easier to read.
  • Works with normal String APIs.
  • Supports String formatting.

21. Limitations

  • Requires Java 15 or later as a standard feature.
  • Whitespace behavior should be understood when formatting important text.
  • Text blocks are not a separate runtime data type; they produce Strings.
  • Very short strings are usually clearer with ordinary string literals.

22. Example ๐ŸŒ๐Ÿซ 

Suppose a Java application needs to generate an email template. A text block makes the template much easier to maintain.

Java
String customerName = "Samiksha";

String email = """
        Dear %s,

        Welcome to CIIT Institute.

        We are happy to have you
        learning Java with us.

        Regards,
        CIIT Institute
        """.formatted(customerName);

System.out.println(email);

23. Best Practices

  • Use text blocks for genuinely multi-line content.
  • Use normal strings for short single-line values.
  • Keep indentation consistent.
  • Use formatted() for readable value substitution.
  • Be careful when whitespace has semantic meaning.
  • Use the Java version required by the project.

24. Interview Questions

Text blocks are multi-line String literals designed to make multi-line text easier to write and read.

Text blocks became a standard feature in Java 15.

A text block produces a normal Java String.

They are useful for HTML, JSON, SQL, XML, email templates, configuration content, and other multi-line strings.

formatted() allows values to be inserted into a String using format specifiers such as %s and %d.

Summary

In this lesson, you learned:

  • What Java text blocks are
  • Text block syntax
  • Automatic indentation
  • Multi-line HTML and JSON
  • Multi-line SQL
  • Escape sequences
  • String formatting with text blocks
  • Whitespace and line continuation
  • Advantages and limitations
  • Real-world text block usage