Sub topic 7
Writing Multi-Line Strings with Text Blocks
This chapter assumes basic Java syntax, loops, methods, and the String class. You should also know how to compile and run a Java program. Text blocks require JDK 15 or later.
A text block is a Java String written across several source-code lines. It is useful for JSON, SQL, HTML, messages, and other text where ordinary string literals would need many \n and \" escape sequences.
Text Block Syntax
A text block starts and ends with three double quotes. The opening delimiter must be followed by a line break.
public class TextBlockExample {
public static void main(String[] args) {
String json = """
{
"name": "Ada",
"age": 36
}
""";
System.out.print(json);
}
}The output is:
{
"name": "Ada",
"age": 36
}The value is still an ordinary String. You can call methods such as length, strip, and formatted on it.
A text block normally ends with a line break. In the example, the value is equivalent to this string:
String json = "{\n \"name\": \"Ada\",\n \"age\": 36\n}\n";Text blocks make the source easier to read because double quotes inside the text do not need escaping.
How Indentation Is Stripped
Java removes incidental indentation from each line. It uses the position of the closing delimiter to help decide which spaces belong to the Java source layout and which spaces belong to the string.
String message = """
Hello
Indented
""";The closing delimiter is indented by eight spaces. Java removes those eight spaces from each content line:
Hello
IndentedThe two extra spaces before Indented remain because they are part of the text.
The closing delimiter can be placed farther to the left when you want leading spaces in the result:
String message = """
Hello
World
""";Here, the closing delimiter starts at the left margin. The two spaces before Hello remain in the string, and the four spaces before World remain as well.
The position of the closing delimiter is therefore part of the formatting decision.
Line Breaks and Trailing Spaces
A line break written inside a text block becomes a line break in the resulting string. The default line terminator is \n, regardless of the platform.
A backslash at the end of a source line suppresses that line break. This is called a line continuation.
String line = """
This is one \
long line.
""";
System.out.print(line);The output is:
This is one long line.The indentation after the backslash does not become a run of spaces. The two source lines join into one line.
A text block usually removes trailing spaces because they are easy to overlook in source code. Use \s when a trailing space must remain.
String fixedWidth = """
key:\s
""";
System.out.print(fixedWidth);The \s escape represents one space. In this example, the first line ends with key: followed by a space.
Double quotes do not normally need escaping:
String quote = """
She said, "Use a text block."
""";Use \" only when you specifically need an escape in the source. A backslash can also escape a backslash when the resulting text must contain one.
Predicting the Exact Content
To predict a text block's value, inspect it in this order:
- Record every line break between the delimiters.
- Find the indentation of the closing delimiter.
- Remove that amount of indentation from each nonblank line.
- Keep any extra spaces.
- Apply escapes such as
\sand line continuation. - Include the final line break unless the text block prevents it.
For example:
String value = """
A
B\
C
""";The line containing B continues directly into the next line. After indentation removal, the value is:
A
BCThere is still a final line break after C.
>callout note > Treat a text block as a String value, not as a special output format. Java processes its indentation and escapes when it creates the string. The receiving API sees only the final characters.
Exercise
What exact text does this code print?
Show answer
It prints first followed by a newline, then two spaces followed by second and another newline:
first
second
The closing delimiter is indented by eight spaces, and the two additional spaces before second remain.
String text = """
first
second
""";
System.out.print(text);