Type
Wrappers
As you know, Java uses
primitive types (also called simple types), such as int or double, to hold
the basic data types supported by the language. Primitive types, rather than
objects, are used for these quantities for the sake of performance. Using
objects for these values would add an unacceptable overhead to even the
simplest of calculations. Thus, the primitive types are not part of the object
hierarchy, and they do not inherit Object.
Despite the performance
benefit offered by the primitive types, there are times when you will need an
object representation. For example, you can’t pass a primitive type by
reference to a method. Also, many of the standard data structures implemented
by Java operate on objects, which means that you can’t use these data
structures to store primitive types. To handle these (and other) situations,
Java provides type wrappers, which
are classes that encapsulate a primitive type within an object. The type
wrapper classes are described in detail in Part II, but they are introduced
here because they relate directly to Java’s autoboxing feature.
The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and
Boolean. 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
Character is a wrapper around a char.
The constructor for Character is Character(char ch)
Here, ch specifies the character that will be wrapped by the Character object being created. To
obtain the char value contained in a
Character object, call charValue( ), shown here:
char charValue( )
It returns the encapsulated
character.
Boolean
Boolean is a wrapper around boolean values.
It defines these constructors:
Boolean(boolean boolValue)
Boolean(String boolString)
In the first version, boolValue must be either true or false. In the second version, if boolString contains the string "true" (in uppercase or
lowercase), then the new Boolean
object will be true. Otherwise, it will be false.
To obtain a boolean value from a Boolean object, use booleanValue( ), shown here: boolean
booleanValue( )
It returns the boolean equivalent of the invoking
object.
The
Numeric Type Wrappers
By far, the most commonly
used type wrappers are those that represent numeric values. These are Byte, Short, Integer, Long, Float, and Double. All
of the numeric type wrappers inherit the abstract class Number. Number declares
methods that return the value of an object in 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 a double, floatValue( ) returns the value as a float, and so on. These methods are
implemented by each of the numeric type wrappers.
All of the numeric type
wrappers define constructors that allow an object to be constructed from a
given value, or a string representation of that value. For example, here are
the constructors defined for Integer:
Integer(int num) Integer(String str)
If str does not contain a valid numeric value, then a NumberFormatException is thrown. All of
the type wrappers override toString( ).
It returns the human-readable form of the value contained within the wrapper.
This allows you to output the value by passing a type wrapper object to println( ), for example, without having
to convert it into its primitive type.
The following program
demonstrates how to use a numeric type wrapper to encapsulate a value and then
extract that value.
// Demonstrate a type wrapper.
class Wrap {
public static void main(String args[]) {
Integer iOb = new Integer(100);
int i = iOb.intValue();
System.out.println(i + " " + iOb); //
displays 100 100
}
}
This program wraps the
integer value 100 inside an Integer
object called iOb. The program then
obtains this value by calling intValue(
) and stores the result in i.
The process of encapsulating
a value within an object is called boxing.
Thus, in the program, this line boxes the value 100 into an Integer:
Integer iOb = new Integer(100);
The process of extracting a
value from a type wrapper is called unboxing.
For example, the program unboxes the value in iOb with this statement:
int i = iOb.intValue();
The same general procedure
used by the preceding program to box and unbox values has been employed since
the original version of Java. However, since JDK 5, Java fundamentally improved
on this through the addition of autoboxing, described next.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.