Categories: Java

Escape Sequence

Java allows us to have certain non-graphic characters in character constants. (non-graphic characters are those which cannot be typed directly from the keyboard, e.g, backspace, tabs, carriage return, etc.). These characters are represented with the use of an escape sequence, which is represented by a backslash(\) followed by one or more characters.

The following table gives a listing of escape sequences:

Escape CharactersDescription
\t to insert a tab in the text at this point.
\’ to insert a single quote character in the text at this point.
\” to insert a double quote character in the text at this point.
\r to insert a carriage return in the text at this point.
\\to insert a backslash character in the text at this point.
\nto insert a new line in the text at this point.
\fto insert a form feed in the text at this point.
\b to insert a backspace in the text at this point.
Example:
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!\r\"Coderzpy\" "); 
    }
}
Output:
Hello, world!
"Coderzpy" 

Note: also read about the Literals in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 day ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago