Escapes in Text Blocks
Use new escape sequences.
Escapes in Text Blocks is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);
}
}Why Line Continuation Helps
You can format source code across several physical lines while producing a single output line. Useful for long sentences or URLs.
public class Main {
public static void main(String[] args) {
String url = """
https://example.com/\
path/to/\
resource
""";
System.out.print(url);
}
}The \s Space Escape
Trailing spaces are normally stripped. The \s escape inserts a literal space that survives stripping, letting you keep meaningful trailing whitespace.
public class Main {
public static void main(String[] args) {
String text = """
red \s
green\s
""";
for (String line : text.split("\n")) {
System.out.println("|" + line + "|");
}
}
}Preserving Column Alignment
\s is handy for fixed-width tables where trailing spaces matter for alignment.
public class Main {
public static void main(String[] args) {
String table = """
NAME\s\s\sAGE
Alice\s\s30
""";
System.out.print(table);
}
}Standard Escapes Still Work
Classic escapes like \t for tab and \n for newline work inside text blocks too.
public class Main {
public static void main(String[] args) {
String text = """
Col1\tCol2
A\tB
""";
System.out.print(text);
}
}Escaping Triple Quotes
To include three consecutive double quotes inside a text block, escape at least one with \" so it is not mistaken for the closing delimiter.
public class Main {
public static void main(String[] args) {
String text = """
He said \"\"\" loudly.
""";
System.out.print(text);
}
}A Literal Backslash
To put an actual backslash in the output, write \\, exactly as in normal strings. This matters for Windows paths or regex patterns.
public class Main {
public static void main(String[] args) {
String path = """
C:\\Users\\Alice
""";
System.out.print(path);
}
}Combining Continuation and \s
You can mix both new escapes. Here a continuation joins lines while \s ensures a space at the join point.
public class Main {
public static void main(String[] args) {
String sentence = """
Hello\s\
World
""";
System.out.print(sentence);
}
}Avoiding Accidental Newlines
Without continuation, every source line break is a newline. Adding \ at line end removes it, giving you full control of where breaks occur.
public class Main {
public static void main(String[] args) {
String joined = """
part1\
part2\
part3
""";
System.out.println(joined);
}
}Single Quote Needs No Escape
Inside a text block, a single double quote " is fine on its own. Only sequences that could form the closing """ need attention.
public class Main {
public static void main(String[] args) {
String quote = """
She said "hi" to me.
""";
System.out.print(quote);
}
}Quick Check
What does ending a text block line with a single backslash \ do?
Recap
You learned text block escapes:
\at line end = line continuation (no newline)\s= a space that survives trailing-whitespace stripping- Standard
\t,\n,\\still work - Escape a quote to embed
""" - A lone
"needs no escaping
Frequently asked questions
Is the “Escapes in Text Blocks” lesson free?
Yes — the full text of “Escapes in Text Blocks” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Escapes in Text Blocks”?
Use new escape sequences. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Escapes in Text Blocks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Text Blocks
- Indentation and Stripping
- Escapes in Text Blocks
- Use Cases: JSON and SQL