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

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

22 hours ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

3 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

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

1 month ago

Estimate π Using Monte Carlo Method

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

2 months ago