Java char type can store Unicode character set.
A literal character is represented inside a pair of single quotes.
All of the visible ASCII characters can be directly entered inside the quotes, such as 'a', 'z', and '@'.
For characters that cannot be entered directly, we can use escape sequences.
For example, \n
is for new line character.
We can directly enter a character in octal or hexadecimal.
For octal notation, use the backslash followed by the three-digit number.
For example, \141
is the letter 'a'.
For hexadecimal, you enter a backslash-u \u
, then exactly four hexadecimal digits.
For example, \u0061
is the ISO-Latin-1 'a'.
The following table shows the character escape sequences.
Escape Sequence | Description |
---|---|
\ddd | Octal character (ddd) |
\uxxxx | Hexadecimal Unicode character (xxxx) |
\' | Single quote |
\" | Double quote |
\\ | Backslash |
\r | Carriage return |
\n | New line (also known as line feed) |
\f | Form feed |
\t | Tab |
\b | Backspace |
Escape Sequences and its Unicode
Escape Sequence | Name | Unicode Code | Decimal Value |
---|---|---|---|
\b | Backspace | \u0008 | 8 |
\t | Tab | \u0009 | 9 |
\n | Linefeed | \u000A | 10 |
\f | Formfeed | \u000C | 12 |
\r | Carriage Return | \u000D | 13 |
\\ | Backslash | \u005C | 92 |
\" | Double Quote | \u0022 | 34 |