Escape Characters in Strings in Java
An escape character lets a string literal contain something that couldn't otherwise be typed into it directly — a newline, a tab, a literal quote mark, or a literal backslash itself. Each one starts with a backslash followed by a specific letter: \n for a newline, \t for a tab, \" for a literal double quote inside a double-quoted string, and \\ for a single literal backslash.
Without \", a string couldn't contain a double-quote character at all, since Java would read that quote as the string's closing one. Without \\, there would be no way to write a literal backslash, since a lone backslash always signals the start of an escape sequence rather than standing for itself.
The newline escape
\n inserts a line break inside a string, so one println call can produce output spanning multiple lines.
The tab escape
\t inserts a horizontal tab — useful for lining up short labels without manually counting spaces.
The escaped quote
A backslash before a double quote, written \", lets that quote appear inside a double-quoted string instead of ending it early.
The escaped backslash
Doubling the backslash, written \\, produces exactly one literal backslash in the resulting string — needed for Windows-style file paths and regular expressions.
Escapes resolve at compile time
By the time a String exists in memory, it just holds real characters — a real newline, a real tab, a real backslash. No backslashes survive from the source code into the running value.
public class EscapeDemo { public static void main(String[] args) { System.out.println("Name:\tGrace\nRole:\tEngineer"); }}The Newline Escape
\n breaks a single string literal across multiple printed lines.
System.out.println("First\nSecond");The Tab Escape
\t inserts a horizontal tab, handy for rough alignment without counting spaces.
System.out.println("A\tB");The Escaped Quote
A backslash before a double quote lets that quote sit inside a double-quoted string instead of ending it early.
System.out.println("She said \"hi\"");The Escaped Backslash
Doubling the backslash produces one literal backslash in the string — essential for Windows-style paths and regular expressions.
System.out.println("C:\\Users");Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.