Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

in detail in Part II, but they are introduced here because they relate directly to Java’s
autoboxing feature.
The type wrappers areDouble,Float,Long,Integer,Short,Byte,Character, andBoolean.
These classes offer a wide array of methods that allow you to fully integrate the primitive
types into Java’s object hierarchy. Each is briefly examined next.


Character
Characteris a wrapper around achar. The constructor forCharacteris


Character(charch)

Here,chspecifies the character that will be wrapped by theCharacterobject being created.
To obtain thecharvalue contained in aCharacterobject, callcharValue( ), shown here:


char charValue( )

It returns the encapsulated character.


Boolean
Booleanis a wrapper aroundbooleanvalues. It defines these constructors:


Boolean(booleanboolValue)
Boolean(StringboolString)

In the first version,boolValuemust be eithertrueorfalse. In the second version, ifboolString
contains the string “true” (in uppercase or lowercase), then the newBooleanobject will be
true. Otherwise, it will be false.
To obtain abooleanvalue from aBooleanobject, usebooleanValue( ), shown here:


boolean booleanValue( )

It returns thebooleanequivalent of the invoking object.


The Numeric Type Wrappers
By far, the most commonly used type wrappersare those that representnumericvalues.
These areByte,Short,Integer,Long,Float, andDouble. All of the numeric type wrappers
inherit the abstract classNumber.Numberdeclares methods that return the value of an
objectin each of the different number formats. These methods are shown here:


byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )

For example,doubleValue( )returns the value of an object as adouble,floatValue( )
returns the value as afloat, and so on. These methods are implemented by each of the
numeric type wrappers.


Chapter 12: Enumerations, Autoboxing, and Annotations (Metadata) 265

Free download pdf