Escape Sequences

String and character literals can use special escape sequences to represent special characters or symbols.

Escape Sequence

Explanation

\b

Backspace

\t

Horizontal tab

\n

New line

\f

Form feed

\r

Carriage return

\”

Double quote

\’

Single quote

\\

Backslash

For example, here is a statement that displays two lines of text on the console:

System.out.println(“Hello\nWorld!”);

In this example, a new line character (\n) is inserted between the words Hello and World!. The resulting output will look like this:

Hello

World!

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.