Sub topic 9
Escapes: Line Continuation, `\s` and Quotes
Assume you know basic Java variables, loops, methods, and the String class. This chapter uses text blocks, which require JDK 15 or later. A text block is a multi-line string literal that starts and ends with three double-quote characters.
Text blocks make formatted text easier to read in source code. Their escape sequences also help you control line breaks, spaces, backslashes, and quotation marks.
Line Continuation
Normally, each physical line inside a text block becomes a line in the resulting string. A backslash at the end of a source line suppresses that line break. This is called a line continuation.
public class LineContinuationDemo {
public static void main(String[] args) {
String message = """
This is one \
long line.
""";
System.out.print(message);
}
}The output is:
This is one long line.The backslash joins the two source lines. The indentation before long line. does not become part of the result.
The backslash must be immediately before the line break. If spaces occur after the backslash, Java no longer treats it as a line-continuation escape.
Line continuation is useful when a long logical line would make the source code too wide:
String query = """
SELECT name, email \
FROM users \
WHERE active = true
""";The resulting string contains one line:
SELECT name, email FROM users WHERE active = trueUse this feature carefully. If the text is meant to contain separate lines, do not add a backslash.
Preserving Trailing Spaces with `\s`
The escape sequence \s represents one ordinary space character. In a text block, it has a special practical use: it preserves a space at the end of a line.
Java removes incidental indentation and trailing spaces from text block source lines during processing. A source line that visibly ends with spaces can therefore be difficult to maintain. Writing \s makes the intended space explicit.
public class SpaceDemo {
public static void main(String[] args) {
String aligned = """
left\s
right
""";
System.out.print(aligned);
}
}The first line in the resulting string is left followed by one space, then a line break. The \s is converted to a space after the text block's layout processing.
This matters when spaces have meaning, such as fixed-width output:
String row = """
Name: Ada\s
Age: 36
""";The first line has a space after Ada, even though the source formatting process would otherwise remove trailing whitespace.
Do not use \s for indentation that should be removed. Use it only when the resulting string must contain that space.
Quotes Inside Text Blocks
Single quotation marks and ordinary double quotation marks do not need escaping inside a text block. This makes JSON, SQL, HTML, and other structured text easier to write.
public class QuoteDemo {
public static void main(String[] args) {
String json = """
{
"name": "Ada",
"language": "Java"
}
""";
System.out.print(json);
}
}The double quotes around the JSON property names are ordinary characters in the resulting string.
A sequence of three double quotes is different. Three unescaped double quotes mark the end of a text block, so a sequence of three quote characters inside the content must escape at least one quote.
public class TripleQuoteDemo {
public static void main(String[] args) {
String marker = """
\"\"\"
""";
System.out.print(marker);
}
}The output is:
"""The backslashes are syntax for escaping the quotes. They are not included in the resulting string.
A literal backslash also needs an escape. Use two backslashes in the source to produce one backslash:
String path = """
C:\\Users\\Ada\\notes.txt
""";The resulting string contains:
C:\Users\Ada\notes.txtHow Text Block Escapes Are Processed
Text block formatting can be understood as a sequence of steps:
This ordering explains why \s can preserve a trailing space. The escape is interpreted after Java has handled the text block's layout.
When reviewing a text block, distinguish the source layout from the string value. The indentation you see in the Java file may be removed, while an explicit escape such as \s remains meaningful.
Choosing the Escape
Use a line-continuation backslash when two physical source lines should become one logical line. Use \s when a trailing space must remain in the result. Leave ordinary quotation marks unescaped, and escape a quote only when it would otherwise form part of the text block delimiter or when another Java escape requires it.
Exercise
What exact string does this text block produce, including line breaks? String value = """ A\s\ B """;
Show answer
It produces the characters A, one space, B, and a final line break: "A B\n".