Open In App

Java Program to Store Escape Sequence Using Character Literals

Last Updated : 06 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

An Escape Sequence is a character or a sequence of characters, the purpose of which is to implement the characters which they don’t represent literally and which might be otherwise unfeasible to represent. Some examples of such characters would be backspace, newline, horizontal or vertical tab, to name a few. In java, these sequences are represented by a preceding backslash ‘\’ and belong to the Character data type. There are a total of 8 escape sequences in java. They are listed below :

Tab : '\t'
Newline : '\n'
Backspace : '\b'
Form feed : '\f'
Carriage return : '\r'
Backslash : '\\'
Single quote : '\''
Double quote : '\"'

1. Type Casting: Type Casting is the concept of converting a primitive data type into another. There are two types of Type Casting :

  • Widening or Automatic Type Casting: In this process, a primitive data type of lower precedence is automatically converted to another primitive data type of higher precedence. For instance, conversion from int to double.
  • Narrowing or Explicit Type Casting: This is the process of conversion of a primitive data type higher in precedence to the one lower in precedence. For instance, conversion from int to char.

We will be using Explicit Type Casting here. More specifically type conversion from int to char.

2. ASCII Code: ASCII Codes are used to represent characters using numbers. It is a uniform system that allows computers to interpret all characters. ASCII stands for American Standard Code for Information Interchange. For instance, the character has an ASCII code of 97.

There are 2 approaches following which we can store Escape Sequences in Character literals in Java. They are listed as follows :

  1. Using normal escape sequences within single quotes
  2. By converting the ASCII Code for escape sequences to character literals using Explicit Type Conversion

Approach 1:

In this approach, we make use of the simplest method, which is to store escape sequences directly as Character literals.

CODE :

Java




// Java code to store escape sequences
// as character literals
 
class StoreCharacterLiterals {
 
    // the method to print the escape
    // sequences
    static void escapeSequences()
    {
        // declaration of Character
        // literals
        char tab = '\t', backspace = '\b', newline = '\n',
             formFeed = '\f', carriageReturn = '\r',
             singleQuote = '\'', doubleQuote = '\"',
             backSlash = '\\';
 
        // printing the horizontal tab
        System.out.println("This" + tab + " is an "
                           + "implementation of tab");
 
        // implementing the backspace
        System.out.println("This" + backspace + " is an "
                           + "implementation "
                           + "of backspace");
 
        // implementing the newline
        System.out.println("This" + newline + " is an "
                           + "implementation of newline");
 
        // implementing the formfeed
        System.out.println("This" + formFeed + " is an"
                           + " implementation of "
                           + "formfeed");
 
        // implementing the carriage return
        System.out.println("This" + carriageReturn
                           + " is an implementation "
                           + "of Carriage Return");
 
        // printing the single quote
        System.out.println("This" + singleQuote
                           + " is an implementation"
                           + " of single quote");
 
        // printing the double quote
        System.out.println("This" + doubleQuote
                           + " is an implementation "
                           + "of double quote");
 
        // printing the backslash
        System.out.println("This" + backSlash
                           + " is an implementation "
                           + " of backslash");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // creating an object of the class
        StoreCharacterLiterals ob = new StoreCharacterLiterals();
 
        // calling the escapeSequences() method
        ob.escapeSequences();
    }
}


Output

This     is an implementation of tab
This is an implementation of backspace
This
 is an implementation of newline
This
     is an implementation of formfeed
 is an implementation of Carriage Return
This' is an implementation of single quote
This" is an implementation of double quote
This\ is an implementation  of backslash

Approach 2:

Following this approach, we use Explicit Type Conversion from int to char to store the Escape Sequences in the Character literals.

Java




// Java code to store escape sequences
// as character literals
 
class StoreCharacterLiterals {
 
    // the method to print the escape
    // sequences
    static void escapeSequences()
    {
 
        // declaration of Character
        // literals using their ASCII codes
        // ASCII code for tab : 9
        // ASCII code for backspace : 8
        // ASCII code for newline : 10
        // ASCII code for formfeed : 12
        // ASCII code for carriage return : 13
        // ASCII code for single quote : 39
        // ASCII code for double quote : 34
        // ASCII code for backslash : 92
        char tab = 9, backspace = 8, newline = 10,
             formFeed = 12, carriageReturn = 13,
             singleQuote = 39, doubleQuote = 34,
             backSlash = 92;
 
        // printing the horizontal tab
        System.out.println("This" + tab + " is an "
                           + "implementation of tab");
 
        // implementing the backspace
        System.out.println("This" + backspace + " is an "
                           + "implementation "
                           + "of backspace");
 
        // implementing the newline
        System.out.println("This" + newline + " is an "
                           + "implementation of newline");
 
        // implementing the formfeed
        System.out.println("This" + formFeed + " is an"
                           + " implementation of "
                           + "formfeed");
 
        // implementing the carriage return
        System.out.println("This" + carriageReturn
                           + " is an implementation "
                           + "of Carriage Return");
 
        // printing the single quote
        System.out.println("This" + singleQuote
                           + " is an implementation"
                           + " of single quote");
 
        // printing the double quote
        System.out.println("This" + doubleQuote
                           + " is an implementation "
                           + "of double quote");
 
        // printing the backslash
        System.out.println("This" + backSlash
                           + " is an implementation "
                           + " of backslash");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // creating an object of the class
        StoreCharacterLiterals ob = new StoreCharacterLiterals();
 
        // calling the escapeSequences() method
        ob.escapeSequences();
    }
}


Output

This     is an implementation of tab
This is an implementation of backspace
This
 is an implementation of newline
This
     is an implementation of formfeed
 is an implementation of Carriage Return
This' is an implementation of single quote
This" is an implementation of double quote
This\ is an implementation  of backslash

Note: Run the code in the console(Windows) or terminal(Ubuntu). The code outputs on a console for \b. For more info, read: https://stackoverflow.com/questions/3328824/java-backspace-escape



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads