Java Reference
In-Depth Information
This statement makes the names of all the static members of the Math class available for use in your pro-
gram code without having to qualify them with the class name. This includes constants such as PI as well as
static methods. You can try this statement in the PondRadius example. With this statement at the beginning
of the source file, you are able to remove the qualification by the class name Math from all the members of
this class that the program uses.
The * in the statement indicates that all static names are to be imported. If you want to import just the
names from the Math class that the PondRadius program uses, you write the following:
import static java.lang.Math.floor; // Import floor
import static java.lang.Math.sqrt; // Import sqrt
import static java.lang.Math.round; // Import round
import static java.lang.Math.PI; // Import PI
These statements import individually the four names from the Math class that the program references.
You could use these four statements at the beginning of the program in place of the previous import state-
ment that imports all the static names. I will discuss this form of the import statement further in Chapter 5.
STORING CHARACTERS
Variables of type char store a single character code. They each occupy 16 bits (2 bytes) in memory because
all characters in Java are stored as Unicode. To declare and initialize a character variable myCharacter you
could use the following statement:
char myCharacter = 'X';
This initializes the variable with the Unicode character representation of the letter X . You must always put
single quotes as delimiters for a character literal in a statement as in this example, 'X' . This is necessary to
enable the compiler to distinguish between the character 'X' and a variable with the name X . Note that you
can't use double quotes as delimiters here because they are used to delimit a character string. A character
string such as "X" is quite different from the literal of type char , 'X' .
Character Escape Sequences
In general, the characters that you are able to enter directly from your keyboard are a function of the keys
you have available and the set of character codes they map to according to your operating system. Whatever
that is, it is a small subset of the characters defined by the Unicode encoding. To enable you to enter any
Unicode character as part of your program source code you can define Unicode characters by specifying the
hexadecimal representation of the character codes in an escape sequence . An escape sequence is simply an
alternative means of specifying a character that is often, but not exclusively, by its code. A backslash indic-
ates the start of an escape sequence, so you have already met the escape sequence for a newline character,
\n .
You create an escape sequence for a Unicode character by preceding the four hexadecimal digits of the
character code by \u . Because the Unicode coding for the letter X is the hexadecimal value 0x0058 (the low
order byte is the same as the ASCII code), you could also declare and define myCharacter with this state-
ment:
Search WWH ::




Custom Search