⏱️ 6 min read • Beginner Level • Lesson 15
In Java, escape sequences are special character combinations
that start with a backslash (\). They are used inside
character literals and string literals to represent
special characters such as newline, tab, quotes, and backslash.
Before learning escape sequences, you should understand Java literals, Java data types, and your first Java program.
Escape sequences are used to insert special characters into Java strings and characters. These characters may have special meaning or may be difficult to type directly in source code.
Escape sequences always begin with a backslash character (\).
They are commonly used in String and char literals.
They help format output, print quotes, display paths, and represent Unicode characters.
public class QuickEscapeExample {
public static void main(String[] args) {
System.out.println("Hello\nJava");
System.out.println("Welcome\tStudents");
}
}
Output:
Hello Java Welcome Students
Some characters cannot be typed directly inside strings because they either have special meaning in Java or affect formatting. Escape sequences solve this problem.
\n.\t.\".\\.| Escape Sequence | Unicode | Meaning |
|---|---|---|
\b |
\u0008 |
Backspace |
\t |
\u0009 |
Horizontal Tab |
\n |
\u000a |
New Line |
\f |
\u000c |
Form Feed |
\r |
\u000d |
Carriage Return |
\' |
\u0027 |
Single Quote |
\" |
\u0022 |
Double Quote |
\\ |
\u005c |
Backslash |
\n is used to print text on multiple lines.\t is used to align columns in console output.\\ is commonly used when displaying Windows file paths.\" is used when double quotes must appear inside a string.
The following Java program uses \t for tab spacing and
\n for a new line.
public class EscapeSequencesExample {
public static void main(String[] args) {
System.out.print("C Prowess");
System.out.print("\tC++ Prowess");
System.out.print("\nJava Prowess");
}
}
Output:
C Prowess C++ Prowess Java Prowess
\n)
The \n escape sequence moves the output to the next line.
public class NewLineExample {
public static void main(String[] args) {
System.out.println("Hello\nWorld");
}
}
Output:
Hello World
\t)
The \t escape sequence inserts horizontal tab spacing.
public class TabExample {
public static void main(String[] args) {
System.out.println("Java\tProgramming");
}
}
Output:
Java Programming
\\)To print a backslash, write two backslashes inside a string.
public class BackslashExample {
public static void main(String[] args) {
System.out.println("C:\\Program Files\\Java");
}
}
Output:
C:\Program Files\Java
\')
The \' escape sequence is useful when writing a single quote
inside a character literal.
public class SingleQuoteExample {
public static void main(String[] args) {
char quote = '\'';
System.out.println(quote);
}
}
Output:
'
\")
The \" escape sequence is used to print double quotes inside a string.
public class DoubleQuoteExample {
public static void main(String[] args) {
System.out.println("He said, \"Hello\"");
}
}
Output:
He said, "Hello"
\r)
The \r escape sequence moves the cursor to the beginning of the current line.
Its visible behavior may vary depending on the console or editor.
public class CarriageReturnExample {
public static void main(String[] args) {
System.out.println("Hello\rWorld");
}
}
\r may depend on the terminal.
In many consoles, it can overwrite text at the start of the same line.
Java supports Unicode escape sequences using the format \uXXXX,
where XXXX is a four-digit hexadecimal Unicode value.
public class UnicodeEscapeExample {
public static void main(String[] args) {
String copyright = "\u00A9 2026";
char letterA = '\u0041';
System.out.println(copyright);
System.out.println(letterA);
}
}
Output:
© 2026 A
\).\\ when you want to print a backslash.\" when you want to print double quotes inside a string.\u0041 to represent characters.\.\n creates a new line and \t adds tab spacing.\\ prints a backslash, and \" prints double quotes.\uXXXX.🧠 Test your understanding with a quick quiz
Topic: Escape-sequences | Language: Java
System.out.println("ABC\bD");System.out.println("Hello\nWorld");System.out.println("She said, \"Java is fun!\"");System.out.println("Java\tRocks");System.out.println("C:\\Program Files\\Java");🎉 Great job! Continue learning Java step by step.