Java Reference
In-Depth Information
One more point about declaring variables before we move on: It is possible to declare
two or more variables using the same declaration statement. Just separate their names by
commas. For example,
var1
and
var2
could have been declared like this:
Another Data Type
In the preceding program, a variable of type
int
was used. However, a variable of type
int
can hold only whole numbers. Thus, it cannot be used when a fractional component is
required. For example, an
int
variable can hold the value 18, but not the value 18.3. For-
tunately,
int
is only one of several data types defined by Java. To allow numbers with frac-
tional components, Java defines two floating-point types:
float
and
double
, which repres-
ent single- and double-precision values, respectively. Of the two,
double
is the most com-
monly used.
To declare a variable of type
double
, use a statement similar to that shown here:
Here,
x
is the name of the variable, which is of type
double
. Because
x
has a floating-point
type, it can hold values such as 122.23, 0.034, or -19.0.
To better understand the difference between
int
and
double
, try the following program: