Sometimes you have to work with characters that are not listed on the keyboard or with characters that have a special meaning, as symbol as a “newline.” They can not be displayed directly in the source the program code, and to use them we need special techniques that will look at now.
Escaping sequences are literals that represent followers of special characters that define a symbol for some reason can not be displayed directly in the source code. This is for example newline character. They give us a roundabout way (escaping) to write a symbol.
Character literals represent a single Unicode character and appear within a pair of single quotation marks. Special characters (control characters and characters that cannot be printed) are represented by a backslash (\) followed by the character code. Special characters supported by Java.
Examples of characters that can be displayed directly in the source code there are many: double quote, tab, newline, backslash and other. Some most commonly used escaping sequences:
Description | Representation |
Backslash or Left slash | \ \ |
Continuation | \ |
Backspace | \b |
Carriage return | \r |
Form feed | \f |
Horizontal or offset tab | \ t |
Newline | \n |
Single quote | \’ |
Double quote | \ “ |
Unicode character | \ udddd |
Octal character | \ ddd |
The symbol \ ( backslash left ) is called the shield symbol because it allows to be displayed on-screen characters that have special meaning or effect and can not be displayed in the source code .
Here is the Java Example for the program Escape :
class escape { public static void main (String args[]) { System.out.print("Microsoft \r"); /* Above statement will print the string Microsoft on the Screen and by \r, the cursor will return back to the first character position M instead of standing after the character t. Now, whatever next is printed, it will be printed at the place of string Microsoft */ System.out.print("Computers String \b\b\b\b\b"); /* By above statement, Computer String is printed at the place of string Microsoft and after printing it, the cursor (which was standing after the character g of String) will go back 5 character back - one \b will take cursor one character back. Now, whatever next is displayed will be printed at the place of string String */ System.out.print(" \' Canada \' "); /* String Canada will be printed cnclosed in singleQuotes */ System.out.print(" \" USA \" \n "); /* string USA is printed enclosed in the double quotes and By \n, cursor will go onto the next line */ System. out. print(" Want \t to\t learn \t computers"); /* the string Want to learn computers will be printed and Each word is separated by a tab */ } }