Java Reference
In-Depth Information
151
Figure 2
Analyzing an Expression
S ELF C HECK
8. What is the value of 1729 / 100? Of 1729 % 100?
9. Why doesn't the following statement compute the average of s1, s2 ,
and s3 ?
double average = s1 + s2 + s3 / 3; // Error
10. What is the value of Math.sqrt(Math.pow(x, 2) +
Math.pow(y, 2)) in mathematical notation?
C OMMON E RROR 4.1: Integer Division
It is unfortunate that Java uses the same symbol, namely /, for both integer and
floating-point division. These are really quite different operations. It is a common
error to use integer division by accident. Consider this program segment that
computes the average of three integers.
int s1 = 5; // Score of test 1
int s2 = 6; // Score of test 2
int s3 = 3; // Score of test 3
double average = (s1 + s2 + s3) / 3; // Error
Search WWH ::




Custom Search