Java Reference
In-Depth Information
Common Programming Error 4.5
Omitting the braces that delimit a block can lead to logic errors, such as infinite loops. To
prevent this problem, some programmers enclose the body of every control statement in
braces, even if the body contains only a single statement.
Explicitly and Implicitly Converting Between Primitive Types
If at least one grade was entered, line 37 of Fig. 4.10 calculates the average of the grades.
Recall from Fig. 4.8 that integer division yields an integer result. Even though variable
average
is declared as a
double
, if we had written the averaging calculation as
double
average = total / gradeCounter;
it would lose the fractional part of the quotient
before
the result of the division is assigned
to
average
. This occurs because
total
and
gradeCounter
are
both
integers, and integer
division yields an integer result.
Most averages are not whole numbers (e.g., 0, -22 and 1024). For this reason, we cal-
culate the class average in this example as a floating-point number. To perform a floating-
point calculation with integer values, we must
temporarily
treat these values as floating-
point numbers for use in the calculation. Java provides the
unary cast operator
to accom-
plish this task. Line 37 of Fig. 4.10 uses the
(
double
)
cast operator—a unary operator—
to create a
temporary
floating-point copy of its operand
total
(which appears to the right
of the operator). Using a cast operator in this manner is called
explicit conversion
or
type
casting
. The value stored in
total
is still an integer.
The calculation now consists of a floating-point value (the temporary
double
copy of
total
) divided by the integer
gradeCounter
. Java can evaluate only arithmetic expressions
in which the operands' types are
identical
. To ensure this, Java performs an operation
called
promotion
(or
implicit conversion
) on selected operands. For example, in an
expression containing
int
and
double
values, the
int
values are promoted to
double
values for use in the expression. In this example, the value of
gradeCounter
is promoted
to type
double
, then floating-point division is performed and the result of the calculation
is assigned to
average
. As long as the
(double)
cast operator is applied to
any
variable in
the calculation, the calculation will yield a
double
result. Later in this chapter, we discuss
all the primitive types. You'll learn more about the promotion rules in Section 6.7.
Common Programming Error 4.6
A cast operator can be used to convert between primitive numeric types, such as
int
and
double
, and between related reference types (as we discuss in Chapter 10, Object-Orient-
ed Programming: Polymorphism and Interfaces). Casting to the wrong type may cause
compilation errors or runtime errors.
A cast operator is formed by placing parentheses around any type's name. The oper-
ator is a
unary operator
(i.e., an operator that takes only one operand). Java also supports
unary versions of the plus (
+
) and minus (
-
) operators, so you can write expressions like
-
7
or
+5
. Cast operators associate from
right to left
and have the same precedence as other
unary operators, such as unary
+
and unary
-
. This precedence is one level higher than that
of the
multiplicative operators
*
,
/
and
%
. (See the operator precedence chart in
Appendix A.) We indicate the cast operator with the notation
(
type
)
in our precedence
charts, to indicate that any type name can be used to form a cast operator.