Java Reference
In-Depth Information
As the name object wrapper implies, these classes are also used to “wrap” a number in a
Java object, as many parts of the standard API are defined in terms of objects. Later on,
Seeking to a Position within a File shows using an Integer object to save an int 's value to a
file using object serialization, and retrieving the value later.
But I haven't yet mentioned the issues of floating point. Real numbers, you may recall, are
numbers with a fractional part. There is an infinite number of real numbers. A floating-point
number—what a computer uses to approximate a real number—is not the same as a real
number. The number of floating-point numbers is finite, with only 2^32 different bit patterns
for float s, and 2\^64 for double s. Thus, most real values have only an approximate corres-
pondence to floating point. The result of printing the real number 0.3 works correctly, as in:
// RealValues.java
System . out . println ( "The real value 0.3 is " + 0.3 );
results in this printout:
The real value 0.3 is 0.3
But the difference between a real value and its floating-point approximation can accumulate
if the value is used in a computation; this is often called a rounding error . Continuing the
previous example, the real 0.3 multiplied by 3 yields:
The real 0.3 times 3 is 0.89999999999999991
Surprised? Not only is it off by a bit from what you might expect, you will of course get the
same output on any conforming Java implementation. I ran it on machines as disparate as an
AMD/Intel PC with OpenBSD, a PC with Windows and the standard JDK, and on Mac OS
X. Always the same answer.
And what about random numbers? How random are they? You have probably heard the ex-
pression “pseudorandom number generator, or PRNG.” All conventional random number
generators, whether written in Fortran, C, or Java, generate pseudo-random numbers. That is,
they're not truly random! True randomness comes only from specially built hardware: an
analog source of Brownian noise connected to an analog-to-digital converter, for example. [ 22 ]
Your average PC of today may have some good sources of entropy, or even hardware-based
sources of randomness (which have not been widely used or tested yet). However, pseudor-
 
Search WWH ::




Custom Search