Java Reference
In-Depth Information
Table 3.1
Primitives and their wrapper classes.
Primitive
Wrapper
boolean
java.lang.Boolean
byte
java.lang.Byte
char
java.lang.Character
short
java.lang.Short
int
java.lang.Integer
long
java.lang.Long
float
java.lang.Float
double
java.lang.Double
Table 3.1 lists the primitive types and the corresponding wrapper classes. See
the Java 2 Platform API Specification for detailed listings and descriptions of the
methods and constants provided with each of the wrapper classes.
With the exception of
java.lang.Character
and
java.lang.
Integer
, the wrappers have the exact same name as the corresponding primi-
tive type but with the first letter capitalized. The wrappers are normal classes that
inherit from the
Object
superclass like all Java classes (see Chapter 4).
The wrapper constructors create class objects from the primitive types. For
example, for a double floating-point number
d:
double d
=
5.0;
Double wrapper
-
double
=
new Double (d);
Here a
Double
wrapper object is created by passing the
double
value to the
Double
constructor.
In turn, the wrapper provides a method to return the primitive value
double r = wrapper
-
double.doubleValue ();
Each wrapper class has a similar method to access the corresponding primitive
value:
intValue()
for
Integer
,
booleanValue()
for
Boolean
, etc. There
are additional convenience methods to return other types to which the wrapped
primitive could be converted. For example,
Integer.longValue()
returns
the value of the
Integer
as a
long
type,
Integer.byteValue()
returns
the value as a byte type, etc. Since nothing casts to or from a
boolean
primitive
type, the
Boolean
wrapper has only the
booleanValue()
method.
3.7.1 Strings, wrappers and primitives
The wrappers for primitive types also provide a number of useful static methods
including some to convert numbers to strings and vice versa. These are very
useful when reading textual input that needs to be converted to a primitive type.
A common situation where these come in handy involves passing numbers to
Search WWH ::
Custom Search