Learn Java
import com.learnjava.variablesdatatypes.EscapeCharacters;

Escape Characters in Strings in Java

@Concept

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.

@HowItWorks
1

The newline escape

\n inserts a line break inside a string, so one println call can produce output spanning multiple lines.

2

The tab escape

\t inserts a horizontal tab — useful for lining up short labels without manually counting spaces.

3

The escaped quote

A backslash before a double quote, written \", lets that quote appear inside a double-quoted string instead of ending it early.

4

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.

5

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.

@Example
public class EscapeDemo {
public static void main(String[] args) {
System.out.println("Name:\tGrace\nRole:\tEngineer");
}
}
Output
Name: Grace
Role: Engineer
1\t inserts a tab after each label, and the line break between Grace and Role: comes from \n — all resolved before the program ever runs.
2The compiler turns every escape sequence into its real character at compile time, so the running string holds an actual tab and an actual newline, not leftover backslashes.
3println writes that already-resolved text to standard output in one call, which is why a single print statement can still produce two visible lines.
@Variants

The Newline Escape

\n breaks a single string literal across multiple printed lines.

System.out.println("First\nSecond");
Output
First
Second

The Tab Escape

\t inserts a horizontal tab, handy for rough alignment without counting spaces.

System.out.println("A\tB");
Output
A B

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\"");
Output
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");
Output
C:\Users
@UseCases
Formatting multi-line console output without calling println more than once
Building a Windows-style file path as a string, which needs its backslashes doubled
Embedding a quotation mark inside a larger double-quoted string
Debugging a compiler error caused by an unescaped quote that ended a string early

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.