Java Reference
In-Depth Information
What Is Doubled?
How did the floating-point type
double
get its name? Is there another type for floating-point
numbers called “single” that is half as big
?
Something like that is true. There is a type that
uses half as much storage, namely the type
float
. Many programming languages tradition-
ally used two types for floating-point numbers. One type used less storage and was very
imprecise (that is, it did not allow very many significant digits). The second type used
double
the amount of storage and so could be much more precise; it also allowed numbers that
were larger (although programmers tend to care more about precision than about size). The
kind of numbers that used twice as much storage were called
double precision
numbers;
those that used less storage were called
single
precision
. Following this tradition, the type
that (more or less) corresponds to this double precision type in Java was named
double
in
Java. The type that corresponds to single precision in Java was called
float
.
(Actually, the type name
double
was inherited from C++, but this explanation applies to
why the type was named
double
in C++, and so ultimately it is the explanation of why the
type is called
double
in Java.)
Constants for strings of characters are given in double quotes, as illustrated by the
following line taken from Display 1.1:
System.out.println("Welcome to Java.");
Be sure to notice that string constants are placed inside of double quotes, while con-
stants of type
char
are placed inside of single quotes. The two kinds of quotes mean
different things. In particular,
'A'
and
"A"
mean different things.
'A'
is a value of type
char
and can be stored in a variable of type
char
.
"A"
is a string of characters. The fact
that the string happens to contain only one character does
not
make the string
"A"
a
value of type
char
. Also notice that, for both strings and characters, the left and right
quotes are the same. We will have more to say about strings later in this chapter.
The type
boolean
has two constants,
true
and
false
. These two constants may be
assigned to a variable of type
boolean
or used anyplace else an expression of type
bool-
ean
is allowed. They must be spelled with all lowercase letters.
quotes
Arithmetic Operators and Expressions
As in most other languages, Java allows you to form expressions using variables, con-
stants, and the arithmetic operators:
+
(addition),
−
(subtraction),
*
(multiplica-
tion),
/
(division),
%
(modulo, remainder). These expressions can be used anyplace it is
legal to use a value of the type produced by the expression.
All of the arithmetic operators can be used with numbers of type
int
, numbers of
type
double
, and even with one number of each type. However, the type of the value
produced and the exact value of the result depends on the types of the numbers being
mixing types