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), but the newline and double quote characters that must be represented with escape sequences in a string literal (§3.10.5) 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.4, and §3.6 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).
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), 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) 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:
-
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.
-
Incidental white space is removed, as if by execution of
String.stripIndenton the characters resulting from step 1. -
Escape sequences are interpreted, as if by execution of
String.translateEscapeson 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 developers 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) - are "interned" so as to share unique instances, as if by execution of the method String.intern (§12.5).
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), 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) { ... }