Java Reference
In-Depth Information
used Han (Chinese) ideographs, occupy 21 bits and cannot be represented in a sin‐
gle char value. Instead, you must use an int value to hold the codepoint of a sup‐
plementary character, or you must encode it into a so-called “surrogate pair” of two
char values.
Unless you commonly write programs that use Asian languages, you are unlikely to
encounter any supplementary characters. If you do anticipate having to process
characters that do not fit into a char , methods have been added to the Character ,
String , and related classes for working with text using int codepoints.
a x
String literals
In addition to the char type, Java also has a data type for working with strings of
text (usually simply called strings ). The String type is a class, however, and is not
one of the primitive types of the language. Because strings are so commonly used,
though, Java does have a syntax for including string values literally in a program. A
String literal consists of arbitrary text within double quotes (as opposed to the sin‐
gle quotes for char literals). For example:
"Hello, world"
"'This' is a string!"
String literals can contain any of the escape sequences that can appear as char liter‐
als (see Table 2-2 ). Use the \ " sequence to include a double quote within a String
literal. Because String is a reference type, string literals are described in more detail
later in this chapter in “Object Literals” on page 74 . Chapter 9 contains more details
on some of the ways you can work with String objects in Java.
Integer Types
The integer types in Java are byte , short , int , and long . As shown in Table 2-1 ,
these four types differ only in the number of bits and, therefore, in the range of
numbers each type can represent. All integral types represent signed numbers; there
is no unsigned keyword as there is in C and C++.
Literals for each of these types are written exactly as you would expect: as a string of
decimal digits, optionally preceded by a minus sign. 1 Here are some legal integer
literals:
0
1
123
- 42000
Integer literals can also be expressed in hexadecimal, binary, or octal notation. A lit‐
eral that begins with 0x or 0X is taken as a hexadecimal number, using the letters A
to F (or a to f ) as the additional digits required for base-16 numbers.
1 Technically, the minus sign is an operator that operates on the literal, but is not part of the literal
itself.
 
Search WWH ::




Custom Search