Numeric Wrapper Classes

The numeric wrapper classes Byte, Short, Integer, Long, Float, and Double are all subclasses of the abstract class Number (Figure 8.1, p. 425).

Each numeric wrapper class defines an assortment of constants, including the minimum and maximum values of the corresponding primitive data type:

Click here to view code image

NumericWrapperType
.MIN_VALUE
NumericWrapperType
.MAX_VALUE

The following code retrieves the minimum and maximum values of various numeric types:

Click here to view code image

byte   minByte   = Byte.MIN_VALUE;     // -128
int    maxInt    = Integer.MAX_VALUE;  // 2147483647
double maxDouble = Double.MAX_VALUE;   // 1.7976931348623157e+308

Converting Numeric Wrapper Objects to Numeric Primitive Types

Each numeric wrapper class defines the following set of type Value() methods for converting the primitive value in the wrapper object to a value of any numeric primitive type:

Click here to view code image

byte   byteValue()
short  shortValue()
int    intValue()
long   longValue()
float  floatValue()
double doubleValue()
See also (4b) in
Figure 8.2
.

The following code shows conversion of values in numeric wrapper objects to any numeric primitive type:

Click here to view code image

Byte    byteObj2   = (byte)16;                  // Cast mandatory
Integer intObj5    = 42030;
Double  doubleObj4 = Math.PI;
short  shortVal  = intObj5.shortValue();        // (1)
long   longVal   = byteObj2.longValue();
int    intVal    = doubleObj4.intValue();       // (2) Truncation
double doubleVal = intObj5.doubleValue();

Notice the potential for loss of information at (1) and (2), when the primitive value in a wrapper object is converted to a narrower primitive data type.

Converting Strings to Numeric Values

Each numeric wrapper class defines a static method parse Type (String str), which returns the primitive numeric value represented by the String object passed as an argument. The Type in the method name parseType stands for the name of a numeric wrapper class, except for the name of the Integer class, which is abbreviated to Int.

These methods throw a NumberFormatException if the String parameter is not a valid argument ((5) in Figure 8.2).

Click here to view code image

static
type
 parse
Type
(String str) throws NumberFormatException

Click here to view code image

byte   value1 = Byte.parseByte(“16”);
int    value2 = Integer.parseInt(“2020”);       // parseInt, not parseInteger
int    value3 = Integer.parseInt(“7UP”);        // NumberFormatException.
double value4 = Double.parseDouble(“3.14”);
double value5 = Double.parseDouble(“Infinity”);

For the integer wrapper types, the overloaded static method parseType() can additionally take a second argument, which can specify the base in which to interpret the string representing the signed integer in the first argument. Note that the string argument does not specify the prefix for the number system notation.

Click here to view code image

type
 parse
Type
(String str, int base) throws NumberFormatException

Click here to view code image

byte  value6  = Byte.parseByte(“1010”, 2);       // Decimal value 10
short value7  = Short.parseShort(“12”, 8);       // Decimal value 10
short value8  = Short.parseShort(“012”, 8);      // Decimal value 10
short value9  = Short.parseShort(“\012”, 8);     // NumberFormatException
int   value10 = Integer.parseInt(“-a”, 16);      // Decimal value -10
int   value11 = Integer.parseInt(“-0xa”, 16);    // NumberFormatException
long  value12 = Long.parseLong(“-a”, 16);        // Decimal value -10L


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *