Tino Intro To Java

Last modified: January 18, 2023

Escape Sequences

Since quotation marks are used to enlose Strings in Java:

System.out.println("Hello there!");

How would one print actual quotation marks?

"Hello there!" said George.

The answer to use Java escape sequence. An escape sequence is a command within a String that produces a special character. Here are several Java escape sequences:

name description
\n Newline character
\b Backspace character
\t Tab character
\" Quote character
\\ Backslash character

Examples

Code 1:

System.out.println("Hello\nthere!");

Output 1:

Hello
There

Code 2:

System.out.println("Hello\tthere!");

Output 2:

Hello   there!

Code 3:

System.out.println("\"Hello there!\" said George.");

Output 3:

"Hello there!" said George.

Code 4:

System.out.println("File saved: C:\\My Documents\\Schoolwork\\Essay.doc");

Output 4:

File saved: C:\My Documents\Schoolwork\Essay.doc

Dark Mode

Outline