Java Escape Characters
In this section, we will discuss Java escape characters or escape sequences. Also, we will use these escape sequences or characters in a Java program.
What are escape characters?
In Java, if a character is preceded by a backslash (\) is known as Java escape sequence or escape characters. It may include letters, numerals, punctuations, etc. Remember that escape characters must be enclosed in quotation marks (""). These are the valid character literals. The Java compiler interprets these characters as a single character that adds a specific meaning to the compiler.
List of Java Escape Characters
In Java, there is a total of eight escape sequences that are described in the following table.
Escape Characters |
Description |
\t |
It is used to insert a tab in the text at this point. |
\' |
It is used to insert a single quote character in the text at this point. |
\" |
It is used to insert a double quote character in the text at this point. |
\r |
It is used to insert a carriage return in the text at this point. |
\\ |
It is used to insert a backslash character in the text at this point. |
\n |
It is used to insert a new line in the text at this point. |
\f |
It is used to insert a form feed in the text at this point. |
\b |
It is used to insert a backspace in the text at this point. |
Why do we use escape characters?
Let's understand the uses of escape characters through the following example. Suppose, we have to print the following statement with double quotes:
The following statements do not print Java enclosed in quotation marks.
While we compile the program with the above two statements, the compiler gives errors, as shown below.
In such a case, the compiler needs to be told that quotation marks do not signal the start or end of a string, but instead are to be printed. The following statement prints statements with quotation marks.
Using Escape Characters in Java Program
EscapeCharaterExample.java
Output:
Andrew Garfield
the best way
to communicate
an idea
is to act it out
And\Or
Carriage
Return
Wall Street's
'JavaTpoint'
Unicode Escape Characters
Java also supports Unicode escape characters. A Unicode escape character consists of a backslash (/) followed by one or more u characters and four hexadecimal digits (\uxxxx). Here, \uxxxx represents \u0000 to \uFFFF.
While interpreting the string if the compiler finds something in the Unicode representation, the compiler replaces it with a respective symbol according to the Java specification.
List of Unicode Character or Escape Sequence
The following table describes the widely used Unicode Character Sequence.
Char |
Unicode |
Escape Sequence |
Description |
Special Codes |
|
U+0009 |
\u0009 |
Horizontal Tab |
U+000A |
\u000A |
Line Feed |
U+000D |
\u000D |
Carriage Return / Enter |
U+00A0 |
\u00A0 |
Non-Breaking Space |
Symbols Codes |
& |
U+0026 |
\u0026 |
Ampersand |
• |
U+2022 |
\u2022 |
Bullet |
? |
U+25E6 |
\u25E6 |
White Bullet |
∙ |
U+2219 |
\u2219 |
Bullet Operator |
‣ |
U+2023 |
\u2023 |
Triangular Bullet |
⁃ |
U+2043 |
\u2043 |
Hyphen Bullet |
° |
U+00B0 |
\u00B0 |
Degree |
∞ |
U+221E |
\u221E |
Infinity |
Currency Codes |
$ |
U+0024 |
\u0024 |
Dollar |
€ |
U+20AC |
\u20AC |
Euro |
£ |
U+00A3 |
\u00A3 |
Pound |
¥ |
U+00A5 |
\u00A5 |
Yen / Yuan |
¢ |
U+00A2 |
\u00A2 |
Cent |
₹ |
U+20B9 |
\u20B9 |
Indian Rupee |
₨ |
U+20A8 |
\u20A8 |
Rupee |
₱ |
U+20B1 |
\u20B1 |
Peso |
₩ |
U+20A9 |
\u20A9 |
Korean Won |
฿ |
U+0E3F |
\u0E3F |
Thai Baht |
₫ |
U+20AB |
\u20AB |
Dong |
₪ |
U+20AA |
\u20AA |
Shekel |
Intellectual Property Codes |
© |
U+00A9 |
\u00A9 |
Copyright |
® |
U+00AE |
\u00AE |
Registered Trademark |
℗ |
U+2117 |
\u2117 |
Sound Recording Copyright |
™ |
U+2122 |
\u2122 |
Trademark |
℠ |
U+2120 |
\u2120 |
Service Mark |
Greek Alphabet Codes |
α |
U+03B1 |
\u03B1 |
Small Alpha |
β |
U+03B2 |
\u03B2 |
Small Beta |
γ |
U+03B3 |
\u03B3 |
Small Gamma |
δ |
U+03B4 |
\u03B4 |
Small Delta |
ε |
U+03B5 |
\u03B5 |
Small Epsilon |
ζ |
U+03B6 |
\u03B6 |
Small Zeta |
η |
U+03B7 |
\u03B7 |
Small Eta |
θ |
U+03B8 |
\u03B8 |
Small Theta |
ι |
U+03B9 |
\u03B9 |
Small Iota |
κ |
U+03BA |
\u03BA |
Small Kappa |
λ |
U+03BB |
\u03BB |
Small Lambda |
μ |
U+03BC |
\u03BC |
Small Mu |
ν |
U+03BD |
\u03BD |
Small Nu |
ξ |
U+03BE |
\u03BE |
Small Xi |
ο |
U+03BF |
\u03BF |
Small Omicron |
π |
U+03C0 |
\u03C0 |
Small Pi |
ρ |
U+03C1 |
\u03C1 |
Small Rho |
σ |
U+03C3 |
\u03C3 |
Small Sigma |
τ |
U+03C4 |
\u03C4 |
Small Tau |
υ |
U+03C5 |
\u03C5 |
Small Upsilon |
φ |
U+03C6 |
\u03C6 |
Small Phi |
χ |
U+03C7 |
\u03C7 |
Small Chi |
ψ |
U+03C8 |
\u03C8 |
Small Psi |
ω |
U+03C9 |
\u03C9 |
Small Omega |
Α |
U+0391 |
\u0391 |
Capital Alpha |
Β |
U+0392 |
\u0392 |
Capital Beta |
Γ |
U+0393 |
\u0393 |
Capital Gamma |
Δ |
U+0394 |
\u0394 |
Capital Delta |
Ε |
U+0395 |
\u0395 |
Capital Epsilon |
Ζ |
U+0396 |
\u0396 |
Capital Zeta |
Η |
U+0397 |
\u0397 |
Capital Eta |
Θ |
U+0398 |
\u0398 |
Capital Theta |
Ι |
U+0399 |
\u0399 |
Capital Iota |
Κ |
U+039A |
\u039A |
Capital Kappa |
Λ |
U+039B |
\u039B |
Capital Lambda |
Μ |
U+039C |
\u039C |
Capital Mu |
Ν |
U+039D |
\u039D |
Capital Nu |
Ξ |
U+039E |
\u039E |
Capital Xi |
Ο |
U+039F |
\u039F |
Capital Omicron |
Π |
U+03A0 |
\u03A0 |
Capital Pi |
Ρ |
U+03A1 |
\u03A1 |
Capital Rho |
Σ |
U+03A3 |
\u03A3 |
Capital Sigma |
Τ |
U+03A4 |
\u03A4 |
Capital Tau |
Υ |
U+03A5 |
\u03A5 |
Capital Upsilon |
Φ |
U+03A6 |
\u03A6 |
Capital Phi |
Χ |
U+03A7 |
\u03A7 |
Capital Chi |
Ψ |
U+03A8 |
\u03A8 |
Capital Psi |
Ω |
U+03A9 |
\u03A9 |
Capital Omega |
Unicode Character Sequence Example
UnicodeCharacterExample.java
Output:
"Example of Unicode Character Sequence", © 2021 JavaTpoint
|