Boolean Literals
The primitive data type boolean represents the truth values true and false that are denoted by the reserved literals true and false, respectively.
Character Literals
A character literal is quoted in single quotes (‘). All character literals have the primitive data type char.
A character literal is represented according to the 16-bit Unicode character set, which subsumes the 8-bit ISO Latin-1 and the 7-bit ASCII characters. In Table 2.9, note that digits (0–9), uppercase letters (A–Z), and lowercase letters (a–z) have contiguous Unicode values. A Unicode character can always be specified as a four-digit hexadecimal number (i.e., 16 bits) with the prefix \u.
Table 2.9 Examples of Character Literals
Character literal | Character literal using Unicode value | Character |
‘ ‘ | ‘\u0020’ | Space |
‘0’ | ‘\u0030’ | 0 |
‘1’ | ‘\u0031’ | 1 |
‘9’ | ‘\u0039’ | 9 |
‘A’ | ‘\u0041’ | A |
‘B’ | ‘\u0042’ | B |
‘Z’ | ‘\u005a’ | Z |
‘a’ | ‘\u0061’ | a |
‘b’ | ‘\u0062’ | b |
‘z’ | ‘\u007a’ | z |
‘Ñ’ | ‘\u0084’ | Ñ |
‘å’ | ‘\u008c’ | å |
‘ß’ | ‘\u00a7’ | ß |
Escape Sequences
Certain escape sequences define special characters, as shown in Table 2.10. These escape sequences allow representation of some special characters in character literals, string literals (p. 39), and text blocks (§8.4, p. 458). These escape sequences can be single-quoted to define character literals, or included in string literals and text blocks. For example, the escape sequence \t and the Unicode value \u0009 are equivalent. However, the Unicode values \u000a and \u000d should not be used to represent a newline and a carriage return in the source code. These values are interpreted as line-terminator characters by the compiler and will cause compile-time errors. You should use the escape sequences \n and \r, respectively, for correct interpretation of these characters in the source code.
Table 2.10 Escape Sequences
Escape sequence | Unicode value | Character |
\b | \u0008 | Backspace (BS) |
\t | \u0009 | Horizontal tab (HT or TAB) |
\n | \u000a | Linefeed (LF), also known as newline (NL) |
\f | \u000c | Form feed (FF) |
\r | \u000d | Carriage return (CR) |
\s | \u0020 | Space (SP) |
\Line terminator | _ | Line continuation in a text block |
\’ | \u0027 | Apostrophe-quote, also known as single quote |
\” | \u0022 | Quotation mark, also known as double quote |
\\ | \u005c | Backslash |
We can also use the escape sequence \ddd to specify a character literal as an octal value, where each digit d can be any octal digit (0–7), as shown in Table 2.11. The number of digits must be three or fewer, and the octal value cannot exceed \377; in other words, only the first 256 characters can be specified with this notation.
Table 2.11 Examples of Escape Sequence \ddd
Escape sequence \ddd | Character literal |
‘\141’ | ‘a’ |
‘\46’ | ‘&’ |
‘\60’ | ‘0’ |