Java Reference
In-Depth Information
Table 5-1. Numeric types
Built-in
type
Object wrap-
per
Size of built-in
(bits)
Contents
8
Signed integer
byte
Byte
16
Signed integer
short
Short
32
Signed integer
int
Integer
64
Signed integer
long
Long
32
IEEE-754 floating point
float
Float
64
IEEE-754 floating point
double
Double
16
Unsigned Unicode character
char
Character
n/a
BigInteger
unlimited
Arbitrary-size immutable integer value
n/a
BigDecimal
unlimited
Arbitrary-size-and-precision immutable floating-
point value
As you can see, Java provides a numeric type for just about any purpose. There are four sizes
of signed integers for representing various sizes of whole numbers. There are two sizes of
floating-point numbers to approximate real numbers. There is also a type specifically de-
signed to represent and allow operations on Unicode characters. The primitive numeric types
are discussed here. The “Big” value types are described in Handling Very Large Numbers .
When you read a string representing a number from user input or a text file, you need to con-
vert it to the appropriate type. The object wrapper classes in the second column have several
functions, one of which is to provide this basic conversion functionality—replacing the C
programmer's atoi / atof family of functions and the numeric arguments to scanf .
Going the other way, you can convert any number (indeed, anything at all in Java) to a string
just by using string concatenation. If you want a little bit of control over numeric formatting,
Formatting Numbers shows you how to use some of the object wrappers' conversion
routines. And if you want full control, that recipe also shows the use of NumberFormat and
its related classes to provide full control of formatting.
Search WWH ::




Custom Search