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



Similar Reads

Java Program to Store Unicode Characters Using Character Literals
ASCII table helps to get computation done earlier but with the ongoing passage of time, there emerge several human-readable languages where the constraint with ASCII was it only supports 256 symbols that too of English only. Now to make computations for other languages there emerges a UNICODE system for computation inside the computer that supports
4 min read
Java Program to Illustrate Use of Binary Literals
A binary literal is a number that is represented in 0s and 1s (binary digits). Java allows you to express integral types (byte, short, int, and long) in a binary number system. To specify a binary literal, add the prefix 0b or 0B to the integral value. So, we can express binary digits in a program by assigning them to variables, and the output of t
3 min read
Using underscore in Numeric Literals in Java
A new feature was introduced by JDK 7 which allows writing numeric literals using the underscore character. Basically, they are broken to enhance readability. This feature enables us to separate groups of digits in numeric literals, which improves the readability of code. For instance, if our code contains numbers with many digits, we can use an un
1 min read
Using underscore in Numeric Literals in Java
When Java was introduced, use of underscore in numeric literals was not allowed but from java version 1.7 onwards we can use '_' underscore symbols between digits of numeric literals. You can place underscores only between digits. Do remember there are certain places where we can not place underscores as listed below as follows: At the beginning or
3 min read
Java Numeric Literals with Underscore
A new feature was introduced by JDK 7 which allows writing numeric literals using the underscore character. Numeric literals are broken to enhance the readability. This feature is used to separate a group of digits in numeric literal which can improve the readability of source code. There are some rules the programmer has to follow before using num
2 min read
Array Literals in Java
Literal is just unlikely any constant value that can be assigned to a variable. Illustration: int x = 10; // Here 10 is a literal. Now let us stick to the array literals that are just like primitive and string literals java also allows us to use array literals. Java defines special syntax that allows us to initialize array values literally in our p
3 min read
Java String Literals as Constants or Singletons
String is a sequence of characters, which is widely used in Java Programming. In the Java programming language, strings are objects. In Java, the virtual machine may only create a single String instance in memory, if we use the same string in other string variable declarations. Let us consider illustrations for the sake of understanding which is as
4 min read
Literals in Java
Literal: Any constant value which can be assigned to the variable is called literal/constant. In simple words, Literals in Java is a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named ‘’/count is assigned an integer value in the fo
6 min read
Escape Sequences in Java
A character with a backslash (\) just before it is an escape sequence or escape character. We use escape characters to perform some specific task. The total number of escape sequences or escape characters in Java is 8. Each escape character is a valid character literal. The list of Java escape sequences: Why will we need Escape sequence? Suppose we
5 min read
Java program to store a Student Information in a File using AWT
Swing is a part of the JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components which allow a high level of customization and provide rich functionalities, and is used to create window-based applications. Java swing components are lightweight, platform-in
4 min read
Article Tags :
Practice Tags :