0Pricing
Java Academy · Lesson

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

  1. Declaring Text Blocks
  2. Indentation and Stripping
  3. Escapes in Text Blocks
  4. Use Cases: JSON and SQL
← Back to Java Academy