---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 20 Edition"
java_version: 20
chapter: "Chapter 3. Lexical Structure"
section: "3.10.6"
heading: "3.10.6. Text Blocks"
parent: "3.10. Literals"
source: https://docs.oracle.com/javase/specs/jls/se20/html/jls-3.html#jls-3.10.6
id: "3.10.6"
url: https://jvm.obfuscated.software/jls/se20/3.10.6.html
raw: https://jvm.obfuscated.software/jls/se20/3.10.6.md
---

# 3.10.6. Text Blocks

A *text block* consists of zero or more characters enclosed by opening and closing delimiters. Characters may be represented by escape sequences ([§3.10.7](3.10.7.md)), but the newline and double quote characters that must be represented with escape sequences in a string literal ([§3.10.5](3.10.5.md)) may be represented directly in a text block.

```
TextBlock:
  " " " {TextBlockWhiteSpace} LineTerminator {TextBlockCharacter} " " "

TextBlockWhiteSpace:
  WhiteSpace but not LineTerminator

TextBlockCharacter:
  InputCharacter but not \
  EscapeSequence
  LineTerminator
```

The following productions from [§3.3](3.3.md), [§3.4](3.4.md), and [§3.6](3.6.md) are shown here for convenience:

```
WhiteSpace:
  the ASCII SP character, also known as "space"
  the ASCII HT character, also known as "horizontal tab"
  the ASCII FF character, also known as "form feed"
  LineTerminator

LineTerminator:
  the ASCII LF character, also known as "newline"
  the ASCII CR character, also known as "return"
  the ASCII CR character followed by the ASCII LF character

InputCharacter:
  UnicodeInputCharacter but not CR or LF

UnicodeInputCharacter:
  UnicodeEscape
  RawInputCharacter

UnicodeEscape:
  \ UnicodeMarker HexDigit HexDigit HexDigit HexDigit

RawInputCharacter:
  any Unicode character
```

A text block is always of type `String` ([§4.3.3](4.3.3.md)).

The *opening delimiter* is a sequence that starts with three double quote characters (`"""`), continues with zero or more space, tab, and form feed characters, and concludes with a line terminator.

The *closing delimiter* is a sequence of three double quote characters.

The *content* of a text block is the sequence of characters that begins immediately after the line terminator of the opening delimiter, and ends immediately before the first double quote of the closing delimiter.

Unlike in a string literal ([§3.10.5](3.10.5.md)), it is *not* a compile-time error for a line terminator to appear in the content of a text block.

**Example 3.10.6-1. Text Blocks**

When multi-line strings are desired, a text block is usually more readable than a concatenation of string literals. For example, compare these alternative representations of a snippet of HTML:

```
String html = "<html>\n" +
              "    <body>\n" +
              "        <p>Hello, world</p>\n" +
              "    </body>\n" +
              "</html>\n";

String html = """
              <html>
                  <body>
                      <p>Hello, world</p>
                  </body>
              </html>
              """;
```

The following are examples of text blocks:

```
class Test {
    public static void main(String[] args) {
        // The six characters w i n t e r
        String season = """
                        winter""";

        // The seven characters w i n t e r LF
        String period = """
                        winter
                        """;

        // The ten characters H i , SP " B o b " LF
        String greeting = """
                          Hi, "Bob"
                          """;

        // The eleven characters H i , LF SP " B o b " LF
        String salutation = """
                            Hi,
                             "Bob"
                            """;

        // The empty string (zero length)
        String empty = """
                       """;

        // The two characters " LF
        String quote = """
                       "
                       """;

        // The two characters \ LF
        String backslash = """
                           \\
                           """;
    }
}
```

Using the escape sequences `\n` and `\"` to represent a newline character and a double quote character, respectively, is permitted in a text block, though not usually necessary. The exception is where three consecutive double quote characters appear that are not intended to be the closing delimiter `"""` - in this case, it is necessary to escape at least one of the double quote characters in order to avoid mimicking the closing delimiter.

**Example 3.10.6-2. Escape sequences in text blocks**

In the following program, the value of the `story` variable would be less readable if individual double quote characters were escaped:

```
class Story1 {
    public static void main(String[] args) {
        String story = """
            "When I use a word," Humpty Dumpty said,
            in rather a scornful tone, "it means just what I
            choose it to mean - neither more nor less."
            "The question is," said Alice, "whether you
            can make words mean so many different things."
            "The question is," said Humpty Dumpty,
            "which is to be master - that's all."
        """;
    }
}
```

If the program is modified to place the closing delimiter on the last line of the content, then an error occurs because the first three consecutive double quote characters on the last line are translated ([§3.2](3.2.md)) into the closing delimiter `"""` and thus a stray double quote character remains:

```
class Story2 {
    public static void main(String[] args) {
        String story = """
            "When I use a word," Humpty Dumpty said,
            in rather a scornful tone, "it means just what I
            choose it to mean - neither more nor less."
            "The question is," said Alice, "whether you
            can make words mean so many different things."
            "The question is," said Humpty Dumpty,
            "which is to be master - that's all."""";  // error
    }
}
```

The error can be avoided by escaping the final double quote character in the content:

```
class Story3 {
    public static void main(String[] args) {
        String story = """
            "When I use a word," Humpty Dumpty said,
            in rather a scornful tone, "it means just what I
            choose it to mean - neither more nor less."
            "The question is," said Alice, "whether you
            can make words mean so many different things."
            "The question is," said Humpty Dumpty,
            "which is to be master - that's all.\"""";  // OK
    }
}
```

If a text block is intended to denote another text block, then it is recommended to escape the first double quote character of the embedded opening and closing delimiters:

```
class Code {
    public static void main(String[] args) {
        String text = """
            The quick brown fox jumps over the lazy dog
        """;

        String code =
            """
            String text = \"""
                The quick brown fox jumps over the lazy dog
            \""";
            """;
    }
}
```

The string represented by a text block is *not* the literal sequence of characters in the content. Instead, the string represented by a text block is the result of applying the following transformations to the content, in order:

1. Line terminators are *normalized* to the ASCII LF character, as follows:

   - An ASCII CR character followed by an ASCII LF character is translated to an ASCII LF character.
   - An ASCII CR character is translated to an ASCII LF character.

2. Incidental white space is removed, as if by execution of `String.stripIndent` on the characters resulting from step 1.

3. Escape sequences are interpreted, as if by execution of `String.translateEscapes` on the characters resulting from step 2.

When this specification says that a text block *contains* a particular character or sequence of characters, or that a particular character or sequence of characters is *in* a text block, it means that the string represented by the text block (as opposed to the literal sequence of characters in the content) contains the character or sequence of characters.

**Example 3.10.6-3. Order of transformations on text block content**

Interpreting escape sequences last allows programmers to use `\n`, `\f`, and `\r` for vertical formatting of a string without affecting the normalization of line terminators, and to use `\b` and `\t` for horizontal formatting of a string without affecting the removal of incidental white space. For example, consider this text block that mentions the escape sequence `\r` (CR):

```
String html = """
              <html>\r
                  <body>\r
                      <p>Hello, world</p>\r
                  </body>\r
              </html>\r
              """;
```

The `\r` escape sequences are not interpreted until after the line terminators have been normalized to LF. Using Unicode escapes to visualize LF (`\u000A`) and CR (`\u000D`), and using `|` to visualize the left margin, the string represented by the text block is:

```
|<html>\u000D\u000A
|    <body>\u000D\u000A
|        <p>Hello, world</p>\u000D\u000A
|    </body>\u000D\u000A
|</html>\u000D\u000A
```

At run time, a text block is a reference to an instance of class `String` that denotes the string represented by the text block.

Moreover, a text block always refers to the *same* instance of class `String`. This is because the strings represented by text blocks - or, more generally, strings that are the values of constant expressions ([§15.29](15.29.md)) - are "interned" so as to share unique instances, as if by execution of the method `String.intern` ([§12.5](12.5.md)).

**Example 3.10.6-4. Text blocks evaluate to `String`**

Text blocks can be used wherever an expression of type `String` is allowed, such as in string concatenation ([§15.18.1](15.18.1.md)), in the invocation of methods on instances of `String`, and in annotations with `String` elements:

```
System.out.println("ab" + """
                          cde
                          """);

String cde = """
             abcde""".substring(2);

String math = """
              1+1 equals \
              """ + String.valueOf(2);

@Preconditions("""
    rate > 0 &&
    rate <= MAX_REFRESH_RATE
""")
public void setRefreshRate(int rate) { ... }
```
