Characters in Java are indices into the Unicode character set. They are 16-bit values that
can be converted into integers and manipulated with the integer operators, such as the
addition and subtraction operators. 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 are impossible to enter directly, there are several
escape sequences, which allow you to enter the character you need, such as ‘\’’ for the
single-quote character itself, and ‘\n’ for the newline character. There is also a mechanism
for directly entering the value of 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’ because the top byte is zero. ‘\ua432’ is a Japanese
Katakana character. Table 3-1 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
Table 3-1. Character Escape Sequences
0 comments: