|
All wrapper class has a typeValue() method which returns the value of the corresponding primitive type. For example,
Boolean boolObject = new Boolean(true);
if(boolObject.booleanValue())
System.out.println(“It’s true!”); // printed
The Boolean wrapper class has a booleanValue() method that return boolean value wrapped by the object. Table 13.5 summarizes the typeValue()method signatures and which wrapper classes define these methods.
Table 13.5 The typeValue() methods and the wrapper classes
|
The typeValue() method signatures |
Wrapper classes that have this method |
|
public boolean booleanValue() |
Boolean |
|
public char charValue() |
Character |
|
public byte byteValue() |
Byte Short Integer Long Float Double |
|
public short shortValue() |
Byte Short Integer Long Float Double |
|
public int intValue() |
Byte Short Integer Long Float Double |
|
public long longValue() |
Byte Short Integer Long Float Double |
|
public float floatValue() |
Byte Short Integer Long Float Double |
|
public double doubleValue() |
Byte Short Integer Long Float Double |
The six wrapper classes for numeric types (Byte, Short, Integer, Long, Float and Double) have the typeValue() methods that return a primitive values to any of the six numeric types. Therefore these numeric wrapper classes have six typeValue() methods: - byteValue(), shortValue(), intValue(), longValue, floatValue() and doubleValue(). These six methods make it possible to get any numeric primitive value from a wrapper object of any of the six numeric types.
Integer intObj = new Integer(45); // make a new wrapper object
byte b = intObj.byteValue(); //assigns a byte value 45 to b
short s = intObj.shortValue(); // assigns a short value 45 to s
int i = intObj.intValue(); // assigns a int value 45 to i
long l = intObj.longValue(); // assigns a long value 45L to l
float f = intObj.floatValue(); // assigns a float value 45.0 to f
double d = intObj.doubleValue(); // assigns a double value 45.0 to d
Besides the six typeValue() methods for numeric types, Table 13.5 shows two more methods. The booleanValue() method is defined only in the Boolean wrapper class. Similarly the charValue() method is defined only in the Character wrapper class. Boolean and Character class do not have any other typeValue() method.