In Java, all variables must be declared before they can be used. The basic form of
a variable declaration is shown here:
type identifier [ = value][, identifier [= value] ...] ;
The type is one of Java’s atomic types, or the name of a class or interface. (Class and
interface types are discussed later in Part I of this book.) The identifier is the name of the
variable. You can initialize the variable by specifying an equal sign and a value. Keep
in mind that the initialization expression must result in a value of the same (or compatible)
type as that specified for the variable. To declare more than one variable of the
specified type, use a comma-separated list.
Here are several examples of variable declarations of various types. Note that some
include an initialization.
int a, b, c; // declares three ints, a, b, and c.
int d = 3, e, f = 5; // declares three more ints, initializing
// d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x'; // the variable x has the value 'x'.
The identifiers that you choose have nothing intrinsic in their names that indicates
their type. Many readers will remember when FORTRAN predefined all identifiers
from I through N to be of type INTEGER while all other identifiers were REAL. Java
allows any properly formed identifier to have any declared type.
0 comments: