Escapes in Text Blocks
Use new escape sequences.
New Escapes for Text Blocks
Text blocks support all the usual escapes plus two new ones designed just for them: the line continuation \ and the explicit space \s. These give precise control over output.
Line Continuation with Backslash
Ending a line with \ tells Java not to insert a newline there. The next line joins onto the current one. This lets you wrap long logical lines for readability.
public class Main {
public static void main(String[] args) {
String text = """
This is one \
single long line.
""";
System.out.print(text);
}
}All lessons in this course
- Declaring Text Blocks
- Indentation and Stripping
- Escapes in Text Blocks
- Use Cases: JSON and SQL