Friday, January 29, 2016

Topic 32: Type Wrappers in Java


Despite the performance benefit offered by the primitive types, there are times when we will need an object representation. For example, we can’t pass a primitive type by reference to a method. Java provides type wrappers, which are classes that encapsulate a primitive type within an object.

  • The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean.

  • 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 - 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( )

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( ). This allows us 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 demonstrates how to use a numeric type wrapper to encapsulate a value and then extract that value.

Integer iOb = new Integer(100);
int i = iOb.intValue();

System.out.println(i + " " + iOb); // displays 100 100


The process of encapsulating a value within an object is called boxing. For ex, the following 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 ex, the program unboxes the value in iOb with this statement:

int i = iOb.intValue(); 

 

Click for NEXT article.

 

Please feel free to correct me by commenting your suggestions and feedback.

No comments:

Post a Comment