Escape Sequences in Java

⏱️ 6 min read • Beginner Level • Lesson 15

Lesson 15 of 124 of Java Tutorial

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.


What are Escape Sequences in Java?

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.

Quick Example

QuickEscapeExample.java
Copy Try Download
public class QuickEscapeExample {

    public static void main(String[] args) {

        System.out.println("Hello\nJava");
        System.out.println("Welcome\tStudents");
    }
}
  

Output:

Hello
Java
Welcome    Students

Why are Escape Sequences Used?

Some characters cannot be typed directly inside strings because they either have special meaning in Java or affect formatting. Escape sequences solve this problem.

Example:
  • To move output to a new line, use \n.
  • To add tab spacing, use \t.
  • To print double quotes inside a string, use \".
  • To print a backslash, use \\.

Common Escape Sequences in Java

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

Real World Use Cases

  • \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.
  • Unicode escape sequences are used to display symbols like ©, ₹, ™, and international characters.

Most Frequently Used Escape Sequences:
  • \n → New Line
  • \t → Tab
  • \\ → Backslash
  • \" → Double Quote

Example: Using Escape Sequences

The following Java program uses \t for tab spacing and \n for a new line.

EscapeSequencesExample.java
Copy Try Download
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

More Examples of Escape Sequences

1. New Line Escape Sequence (\n)

The \n escape sequence moves the output to the next line.

NewLineExample.java
Copy Try Download
public class NewLineExample {
    public static void main(String[] args) {
        System.out.println("Hello\nWorld");
    }
}

Output:

Hello
World

2. Tab Escape Sequence (\t)

The \t escape sequence inserts horizontal tab spacing.

TabExample.java
Copy Try Download
public class TabExample {
    public static void main(String[] args) {
        System.out.println("Java\tProgramming");
    }
}

Output:

Java    Programming

3. Backslash Escape Sequence (\\)

To print a backslash, write two backslashes inside a string.

BackslashExample.java
Copy Try Download
public class BackslashExample {
    public static void main(String[] args) {
        System.out.println("C:\\Program Files\\Java");
    }
}

Output:

C:\Program Files\Java

4. Single Quote Escape Sequence (\')

The \' escape sequence is useful when writing a single quote inside a character literal.

SingleQuoteExample.java
Copy Try Download
public class SingleQuoteExample {
    public static void main(String[] args) {
        char quote = '\'';
        System.out.println(quote);
    }
}

Output:

'

5. Double Quote Escape Sequence (\")

The \" escape sequence is used to print double quotes inside a string.

DoubleQuoteExample.java
Copy Try Download
public class DoubleQuoteExample {
    public static void main(String[] args) {
        System.out.println("He said, \"Hello\"");
    }
}

Output:

He said, "Hello"

6. Carriage Return Escape Sequence (\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.

CarriageReturnExample.java
Copy Try Download
public class CarriageReturnExample {
    public static void main(String[] args) {
        System.out.println("Hello\rWorld");
    }
}
Note: The output of \r may depend on the terminal. In many consoles, it can overwrite text at the start of the same line.

Unicode Escape Sequence

Java supports Unicode escape sequences using the format \uXXXX, where XXXX is a four-digit hexadecimal Unicode value.

UnicodeEscapeExample.java
Copy Try Download
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

Important Notes

  • Escape sequences are always introduced by a backslash (\).
  • They are useful for formatting text and inserting special characters.
  • Use \\ when you want to print a backslash.
  • Use \" when you want to print double quotes inside a string.
  • You can use Unicode escapes such as \u0041 to represent characters.

Common Mistakes

  • Forgetting the backslash before escape characters.
  • Using \n outside a string literal.
  • Using single quotes incorrectly.
  • Confusing Unicode escapes with normal characters.
Summary:
  • Escape sequences are special character combinations that begin with \.
  • They are used inside Java string and character literals.
  • \n creates a new line and \t adds tab spacing.
  • \\ prints a backslash, and \" prints double quotes.
  • Unicode escape sequences use the format \uXXXX.

Frequently Asked Questions

Escape sequences in Java are special character combinations that start with a backslash and are used to represent special characters inside strings and character literals.

The \n escape sequence is used to move output to a new line.

The \t escape sequence is used to insert horizontal tab spacing in output.

To print a backslash in Java, use two backslashes like this: \\.

To print double quotes inside a Java string, use the escape sequence \".

A Unicode escape sequence in Java uses the format \uXXXX, where XXXX is a four-digit hexadecimal Unicode value.

Yes, escape sequences can be used in both character literals and string literals in Java.

Next step: Learn Java Operators

🚀 Continue to Java Operators →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Escape-sequences | Language: Java

Q1. Predict the output:
System.out.println("ABC\bD");
Q2. What will be the output of this code?
System.out.println("Hello\nWorld");
Q3. What will be the output?
System.out.println("She said, \"Java is fun!\"");
Q4. What will this code print?
System.out.println("Java\tRocks");
Q5. What will this code output?
System.out.println("C:\\Program Files\\Java");

🎉 Great job! Continue learning Java step by step.