Chapter 5. Numbers
5.0 Introduction
Numbers are basic to just about any computation. They’re used for array indices, temperatures, salaries, ratings, and an infinite variety of things. Yet they’re not as simple as they seem. With floating-point numbers, how accurate is accurate? With random numbers, how random is random? With strings that should contain a number, what actually constitutes a number?
Java has eight built-in, or primitive, types, seven of which can be used to represent numbers. These are summarized in Table 5-1 with their wrapper (object) types, as well as two numeric classes that do not represent primitive types. The eighth primitive, boolean, and its wrapper Boolean, aren’t numeric and can’t be treated as such. Note that unlike languages such as C or Perl, which don’t specify the size or precision of numeric types, Java—with its goal of portability—specifies these exactly and states that they are the same on all platforms.
| Built-in type | Object wrapper | Size of built-in (bits) | Contents |
|---|---|---|---|
|
|
8 |
Signed integer |
|
|
16 |
Signed integer |
|
|
32 |
Signed integer |
|
|
64 |
Signed integer |
|
|
32 |
IEEE-754 floating point (single precision) |
|
|
64 |
IEEE-754 floating point (double precision) |
|
|
16 |
Unsigned Unicode character |
n/a |
|
unlimited |
Arbitrary-size immutable integer value |
n/a |
|
unlimited |
Arbitrary-size-and-precision immutable floating-point value |
As you ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access