Java Program to Store Escape Sequence Using Character Literals
Last Updated :
06 Jun, 2022
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 :
- Using normal escape sequences within single quotes
- 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
class StoreCharacterLiterals {
static void escapeSequences()
{
char tab = '\t' , backspace = '\b' , newline = '\n' ,
formFeed = '\f' , carriageReturn = '\r' ,
singleQuote = '\'' , doubleQuote = '\"' ,
backSlash = '\\' ;
System.out.println( "This" + tab + " is an "
+ "implementation of tab" );
System.out.println( "This" + backspace + " is an "
+ "implementation "
+ "of backspace" );
System.out.println( "This" + newline + " is an "
+ "implementation of newline" );
System.out.println( "This" + formFeed + " is an"
+ " implementation of "
+ "formfeed" );
System.out.println( "This" + carriageReturn
+ " is an implementation "
+ "of Carriage Return" );
System.out.println( "This" + singleQuote
+ " is an implementation"
+ " of single quote" );
System.out.println( "This" + doubleQuote
+ " is an implementation "
+ "of double quote" );
System.out.println( "This" + backSlash
+ " is an implementation "
+ " of backslash" );
}
public static void main(String[] args)
{
StoreCharacterLiterals ob = new StoreCharacterLiterals();
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
class StoreCharacterLiterals {
static void escapeSequences()
{
char tab = 9 , backspace = 8 , newline = 10 ,
formFeed = 12 , carriageReturn = 13 ,
singleQuote = 39 , doubleQuote = 34 ,
backSlash = 92 ;
System.out.println( "This" + tab + " is an "
+ "implementation of tab" );
System.out.println( "This" + backspace + " is an "
+ "implementation "
+ "of backspace" );
System.out.println( "This" + newline + " is an "
+ "implementation of newline" );
System.out.println( "This" + formFeed + " is an"
+ " implementation of "
+ "formfeed" );
System.out.println( "This" + carriageReturn
+ " is an implementation "
+ "of Carriage Return" );
System.out.println( "This" + singleQuote
+ " is an implementation"
+ " of single quote" );
System.out.println( "This" + doubleQuote
+ " is an implementation "
+ "of double quote" );
System.out.println( "This" + backSlash
+ " is an implementation "
+ " of backslash" );
}
public static void main(String[] args)
{
StoreCharacterLiterals ob = new StoreCharacterLiterals();
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
Share your thoughts in the comments
Please Login to comment...