Big Java | Brief Java |
7th Edition | 9th Edition |
![]() |
![]() |
System.out
refers to an object. ” For example, the expression System.out
that you saw in Chapter 1 refers to an object.reg2.purchase
entry from 9.25 to 38.75. (In the source code, you can see that purchase
is the sum of all purchases.)Common Error 4.3 Roundoff Errors
Roundoff errors are a fact of life when calculating with floating-point numbers. You probably have encountered that phenomenon yourself with manual calculations. If you calculate 1 ⁄ 3 to two decimal places, you get 0.33. Multiplying again by 3, you obtain 0.99, not 1.00.
In the processor hardware, numbers are represented in the binary number system, using only digits 0 and 1. As with decimal numbers, you can get roundoff errors when binary digits are lost. They just may crop up at different places than you might expect.
In the binary system, there is no exact representation for 4.35, just as there is no exact representation for 1 ⁄ 3 in the decimal system. The representation used by the computer is just a little less than 4.35, so 100 times that value is just a little less than 435.
double price = 4.35; double quantity = 100; double total = price * quantity; // Should be 100 * 4.35 = 435 System.out.println(total); // Prints 434.99999999999999
You can deal with roundoff errors by rounding to the nearest integer (see Section 4.2.5) or by displaying a fixed number of digits after the decimal separator (see Section 4.3.2).
if (richter >= 4.5) { "Damage to poorly constructed buildings"; }to
if (richter >= 4.5) { description = "Damage to poorly constructed buildings"; }
int[][] counts = { { 0, 3, 0 }, { 0, 0, 1 }, { 0, 0, 1 }, { 1, 0, 0 }, { 0, 0, 1 }, { 3, 1, 1 }, { 0, 1, 0 }, { 1, 0, 1 } };
Math.PI
)” to “Between the names of classes and static variable names (Math.PI
)”a[from]...a[to-1]
of an array a
”public static void sum
to public static int sum
iter.hasNext() |
Returns false because the iterator is at the end of the collection. |
if (iter.hasPrevious()) { s = iter.previous(); } |
hasPrevious returns true because the iterator is not at the beginning of the list. The call to previous returns "Sally" , and the iterator is again at the beginning of the list. previous and hasPrevious are ListIterator methods. |
iter.set("Juliet"); |
The set method updates the last element returned by next or previous , without changing the iterator position. The list is now [Juliet] . |
(k, v) -> v + 1
” to “(p, v) -> p + 1
” (4x)(d, s) -> v + 1
” to “(p, v) -> p + 1
” and “(d, s) -> v + sales
” to “(p, v) -> p + sales
” (3x)if (difference != 0) { return 0; }
to if (difference != 0) { return difference; }
\n
to \r\n
(3x)Thanks to Claire Bono, Hossein Darbandi, Daniel Ford, Stephen Gilbert, Cindy Johnson, Larry Morelli, Waleed Mortaja, Hoang Nguyen, Barry Nichols, Raymond Novak, and (your name might go here) for their bug reports and suggestions.
Please use this form to report any bugs that you find. Please check the list of known bugs first before you report a bug. Unfortunately, I do not have the time to respond personally to every report, but I do read them all and will post updates to this page. Thank you!