Java Reference
In-Depth Information
many sisters do you have?”). Similarly, we realize that weight can vary by tiny
amounts (175 pounds versus 175.5 pounds versus 175.25 pounds, and so on), and we
use the word “much” for these real-number quantities (“How much do you weigh?”).
In programming, this distinction is even more important, because integers and
reals are represented in different ways in the computer's memory: Integers are stored
exactly, while reals are stored as approximations with a limited number of digits of
accuracy. You will see that storing values as approximations can lead to round-off
errors when you use real values.
The name
double
for real values is not very intuitive. It's an accident of history in
much the same way that we still talk about “dialing” a number on our telephones
even though modern telephones don't have dials. The C programming language intro-
duced a type called
float
(short for “floating-point number”) for storing real num-
bers. But
float
s had limited accuracy, so another type was introduced, called
double
(short for “double precision,” meaning that it had double the precision of a
simple float). As memory became cheaper, people began using
double
as the default
for floating-point values. In hindsight, it might have been better to use the word
float
for what is now called
double
and a word like “half” for the values with less
accuracy, but it's tough to change habits that are so ingrained. So, programming lan-
guages will continue to use the word
double
for floating-point numbers, and people
will still talk about “dialing” people on the phone even if they've never touched a
telephone dial.
When you write programs, you will often need to include values and calculations.
The technical term for these elements is
expressions.
Expression
A simple value or a set of operations that produces a value.
The simplest expression is a specific value, like
42
or
28.9
. We call these “literal
values,” or
literals.
More complex expressions involve combining simple values.
Suppose, for example, that you want to know how many bottles of water you have. If
you have two 6-packs, four 4-packs, and two individual bottles, you can compute the
total number of bottles with the following expression:
(2 * 6) + (4 * 4) + 2
Notice that we use an asterisk to represent multiplication and that we use paren-
theses to group parts of the expression. The computer determines the value of an
expression by
evaluating
it.
Evaluation
The process of obtaining the value of an expression.
Search WWH ::
Custom Search