Java Special Characters
Because strings must be written within quotes, Java will misunderstand this string, and generate an error:
String txt = "We are the so-called "Vikings" from the north.";
The solution to avoid this problem, is to use the backslash escape character.
The backslash ( \ ) escape character turns special characters into string characters:
Escape character | Result | Description |
---|---|---|
\’ | ‘ | Single quote |
\» | « | Double quote |
\\ | \ | Backslash |
The sequence \» inserts a double quote in a string:
Example
String txt = "We are the so-called \"Vikings\" from the north.";
The sequence \’ inserts a single quote in a string:
Example
The sequence \\ inserts a single backslash in a string:
Example
String txt = "The character \\ is called backslash.";
Other common escape sequences that are valid in Java are:
Code | Result | Try it |
---|---|---|
\n | New Line | Try it » |
\r | Carriage Return | Try it » |
\t | Tab | Try it » |
\b | Backspace | Try it » |
\f | Form Feed |
Characters
Most of the time, if you are using a single character value, you will use the primitive char type. For example:
char ch = 'a'; // Unicode for uppercase Greek omega character char uniChar = '\u03A9'; // an array of chars char[] charArray = < 'a', 'b', 'c', 'd', 'e' >;
There are times, however, when you need to use a char as an objectfor example, as a method argument where an object is expected. The Java programming language provides a wrapper class that «wraps» the char in a Character object for this purpose. An object of type Character contains a single field, whose type is char . This Character class also offers a number of useful class (that is, static) methods for manipulating characters.
You can create a Character object with the Character constructor:
Character ch = new Character('a');
The Java compiler will also create a Character object for you under some circumstances. For example, if you pass a primitive char into a method that expects an object, the compiler automatically converts the char to a Character for you. This feature is called autoboxingor unboxing, if the conversion goes the other way. For more information on autoboxing and unboxing, see Autoboxing and Unboxing.
Note: The Character class is immutable, so that once it is created, a Character object cannot be changed.
The following table lists some of the most useful methods in the Character class, but is not exhaustive. For a complete listing of all methods in this class (there are more than 50), refer to the java.lang.Character API specification.
Method | Description |
---|---|
boolean isLetter(char ch) boolean isDigit(char ch) | Determines whether the specified char value is a letter or a digit, respectively. |
boolean isWhitespace(char ch) | Determines whether the specified char value is white space. |
boolean isUpperCase(char ch) boolean isLowerCase(char ch) | Determines whether the specified char value is uppercase or lowercase, respectively. |
char toUpperCase(char ch) char toLowerCase(char ch) | Returns the uppercase or lowercase form of the specified char value. |
toString(char ch) | Returns a String object representing the specified character value that is, a one-character string. |
Escape Sequences
A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:
Escape Sequence | Description |
---|---|
\t | Insert a tab in the text at this point. |
\b | Insert a backspace in the text at this point. |
\n | Insert a newline in the text at this point. |
\r | Insert a carriage return in the text at this point. |
\f | Insert a form feed in the text at this point. |
\’ | Insert a single quote character in the text at this point. |
\» | Insert a double quote character in the text at this point. |
\\ | Insert a backslash character in the text at this point. |
When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. For example, if you want to put quotes within quotes you must use the escape sequence, \», on the interior quotes. To print the sentence
System.out.println("She said \"Hello!\" to me.");
Escaping characters in Java | Explained
An escape sequence indicates an alternative interpretation of a series of characters. In Java, a character before a backslash (\) is an escape sequence. It is utilized to perform actions, including inserting the characters on the new line, carriage return, and inserting a tab between string characters. It also provides the facility to represent the characters that have some special meaning in double quotes and so on.
This write-up will state about the escaping characters in Java.
What are Escaping Characters in Java?
Java provides the facility to utilize the escape characters to signal an alternative interpretation of a series of characters. Java provides the facility to precede the characters by using the backslash. The compiler of Java takes an escape sequence as a single character which has some special meaning. There are various escape characters are available in Java listed below:
How to Utilize the Escaping Characters in Java
To use the escaping characters in Java, check out the following examples stated below:
Example 1: Use of \t
“\t” is used to insert a tab between the two words in a defined string. To do so, we will define a string variable and pass a string as the value of the variable:
Invoke the “println()” method and pass the defined string variable as the argument of this method to display the output on the console:
It can be observed that the tab is inserted successfully between the string words:
Example 2: Use of \’
This “\’” escape character splits the Java String into different parts by inserting a single quote. For practical purposes, make a string, and set the “\’” in the string words to separate the words. Then, store this string in a variable:
Invoke the “println()” method to display the result on the screen:
As a result, the string words are separated with the single quote:
Example 3: Use of \n
The “\n” escape character is utilized to move the carriage down in the new blank line. For that purpose, the “\n” is set in between the string words:
Then, print the output on the console with the help of the “println()” method:
Example 4: Use of \”
The \” escape character is used to insert double quotes in the Java String. For demonstration, double quotes are added around the TSL LTD UK:
Example 5: Use of \r
The “\r” is used to move the carriage left in the new line. For that purpose, we will define a string and store it in a variable. Inside the string, we will set the “\r” to move the text to the new blank line:
Example 6: Use of \\
The “\\” is utilized to put a backslash in the string. To do so, the “\\” is placed between the words:
Now, invoke the “println()” method to show the result on the console:
Example 7: Use of \f
For moving to a new page from a certain point in a string while printing, the “\f” escape is used. A small example is shown below:
Then, invoke the “println()” method to show the result on the screen:
That’s all about the escaping characters in Java.
Conclusion
There are various escape characters used in Java, including “\t”, “\’”, “\n”, “\’’”, “\r”, “\\”, “\f”, etc. For example, the “\t” inserts the tab between the string words, and “\n” moves the cursor to the new line. This post has stated most known escape characters in Java.
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 would like to run the subsequent java code:
This code gives a compile time error as :
prog.java:3: error: ')' expected System.out.println("Hi geek, welcome to "GeeksforGeeks"."); ^ prog.java:3: error: not a statement System.out.println("Hi geek, welcome to "GeeksforGeeks"."); ^ prog.java:3: error: ';' expected System.out.println("Hi geek, welcome to "GeeksforGeeks"."); ^ 3 errors
This happened because the compiler expects nothing but only strings inside the quotation mark but when the compiler found a quotation mark, it expects another quotation mark in the near future (the closing one) and between them, the string of text should be created. During this case, the quotation marks of the word “GeeksforGeeks” gets nested(inside another quotation marks). Once the compiler reaches here, the compiler gets confused. According to the rule, the quotation mark suggests the compiler for creating a string but the compiler was busy with doing that thing previously and the code gives us a compile-time error.
So we should provide proper instructions to the compiler about the quotation mark. i.e, when a quotation is used for creating a string(as a command) and when it is a character itself (the part of the output string).
Similar sorts of confusion arise with other characters also(like- backslashes(), single and double quotation mark (‘, ”)) and these also provide a compile-time error in every case. For Solving these sorts of issues we have to use the java character escaping.
Control Sequence:
A control sequence is nothing however the backslash(\) glued with a character (the character which has to be escaped) is called a control sequence.
Example:
\\ is a control sequence used for displaying a backslash as output.
so let’s use this concept in the previous java code to avoid the compile-time error: