Java Reference
In-Depth Information
The next section summarizes these primitive data types. In addition to these primi‐
tive types, Java supports nonprimitive data types known as reference types, which
are introduced in “Reference Types” on page 84 .
The boolean Type
The boolean type represents truth values. This type has only two possible values,
representing the two Boolean states: on or off, yes or no, true or false. Java reserves
the words true and false to represent these two Boolean values.
Programmers coming to Java from other languages (especially JavaScript) should
note that Java is much stricter about its Boolean values than other languages—in
particular, a boolean is neither an integral nor an object type, and incompatible val‐
ues cannot be used in place of a boolean . In other words, you cannot take shortcuts
such as the following in Java:
a x
Object o = new Object ();
int i = 1 ;
if ( o ) {
while ( i ) {
//...
}
}
Instead, Java forces you to write cleaner code by explicitly stating the comparisons
you want:
if ( o != null ) {
while ( i != 0 ) {
// ...
}
}
The char Type
The char type represents Unicode characters. Java has a slightly unique approach to
representing characters— javac accepts identifiers as UTF-8 (a variable-width
encoding) in input, but represents chars internally as a fixed-width encoding that is
16 bits wide.
These distinctions do not normally need to concern the developer, however. In most
cases, all that is required is to remember the rule that to include a character literal in
a Java program, simply place it between single quotes (apostrophes):
char c = 'A' ;
You can, of course, use any Unicode character as a character literal, and you can use
the \u Unicode escape sequence. In addition, Java supports a number of other
escape sequences that make it easy both to represent commonly used nonprinting
ASCII characters such as newline and to escape certain punctuation characters that
have special meaning in Java. For example:
Search WWH ::




Custom Search