Course contentsShow
← Java
Lesson 2235 of 2,87043. String Handling in DepthFree lesson

Text Block Syntax: Triple Quotes

Learn the triple-quote syntax (""" ... """) for declaring text blocks and the rules for opening and closing delimiters.

Text Block Syntax: Triple Quotes

You'll learn how to use the triple-quote delimiter (""") to declare multi-line text blocks in Java.

The Core Syntax

A text block begins with three double-quote characters (""") followed immediately by a line terminator (newline), then contains your multi-line content, and ends with another set of """. Think of the opening delimiter as opening a "gate" — you can't put any text on the same line as that first """. The content must start on the next line.

Here's the basic structure:

String textBlock = """
    This is a text block.
    It can span multiple lines.
    """;

Opening Delimiter Rules

The opening """ must be followed by a line terminator — you cannot write:

String invalid = """This won't work"""; // Compilation error

This rule ensures the text block's content is visually distinct and prevents ambiguity.

Closing Delimiter Rules

The closing """ can appear:

  1. On its own line (most common):

    String poem = """
        Roses are red,
        Violets are blue.
        """;
    
  2. Immediately after the last content line:

    String compact = """
        Single line""";
    

The closing delimiter's position affects indentation handling (covered in future lessons), but syntactically, both placements are valid.

Analogy

Think of """ like opening and closing parentheses in a conversation — the first set signals "I'm about to say something long," the newline clears your throat, then you speak, and finally you close with another set when done.

Key Takeaway: Text blocks use triple-quote delimiters ("""), the opening delimiter requires an immediate newline, and the closing delimiter can appear on its own line or after content.