codescracker


java

Java Type Wrappers



« Previous Tutorial Next Tutorial »


As you know that Java uses the primitive types (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 the 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 the 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 the 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 wrappers are Integer, Short, Double, Long, Float, Byte, Character, and Boolean. These classes offer a wide array of methods that allow you to fully integrate the primitive types into the Java's object hierarchy. Let's examine each.

Character

Character is a wrapper around a char. The constructor for the 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 the charValue(), shown below :

char charValue()

It returns the encapsulated character.

Boolean

Boolean is a wrapper around the boolean values. It defines these constructors:

Boolean(boolean boolValue)
Boolean(String boolString)

In the first version, boolValue must be either true or false. And 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 below :

boolean booleanValue()

It returns the boolean equivalent of the invoking object.

Java Numeric Type Wrappers

By far, the most commonly used type wrappers are those that represent the numeric values. These are Short, Byte, Integer, Float, Long, and Double. All of the numeric type wrappers inherit the abstract class Number. Numbers declares methods that return the value of an object in each of the different number formats. These methods are shown below :

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

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 the constructors that allow an object to be constructed from a given value, or a string representation of that value. For example, following are the constructors defined for the Integer :

Integer(int num)
Integer(String str)

If str does not contain a valid numeric value, then a NumericFormatException is thrown.

All of the type wrappers override the 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 the println().

Java Type Wrappers Example

The following program demonstrates how to use a numeric type wrapper to encapsulate a value and then extract that value:

/* Java Program Example - Java Type Wrappers
 * This program demonstrate a type wrapper  */
 
 class TypeWrapDemo
 {
     public static void main(String args[])
     {
         
         Integer iObj = new Integer(10);
         
         int i = iObj.intValue();
         
         System.out.println(i + "  " + iObj);      // display 10  10
         
     }
 }

Above Java program produce the output :

java type wrappers

This program wraps the integer value 10 inside an Integer object called iObj. The program then obtains this value by calling the intValue() and stores the result in i.

The process of encapsulating a value within an object is called boxing. Thus, in the program, this lines boxes the value 10 into an Integer :

Integer iObj = new Integer(10);

The process of extracting a value from a type wrapper is called unboxing. For example, the program unboxes the value in iObj with this statement:

int i = iObj.intValue();

The same general procedure used by the preceding programs 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 in the next chapter).


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test