Wrappers Classes
Wrappers Classes
Brown
[email protected]
Wrapper Classes
1. Overview
Java provides an object wrapper for each of the eight primitive data types. These
wrappers basically define an object container to store the primitive data type. In addition, the
wrapper classes define constants for the data types (for example, MAX_VALUE, MIN_VALUE),
and provide a suite of methods to convert between data types and perform operations.
...
All the wrapper classes support the following constructors and methods:
• A constructor that accepts a primitive data type and creates an object of the type class.
• A constructor that accepts a single String parameter and creates an object with an initial value
based on the decoded String.
• A data-typeValue method that returns the value of the object as the specified data-type.
• A hashCode() method to compute the has code value for the object.
• A toString() method that generates the string representation of the wrapper object.
The Wrapper classes permit conversion of simple string literals or String objects to the
primitive data types.
• For integer data types use corresponding class method: parseByte, parseShort, parseInt,
parseLong.
• For floating point data types use valueOf to create a wrapper object from a String input and then
use corresponding data-typeValue method to covert the object to the desired primitive data type.
Constants
public static final int MIN_VALUE = 0x80000000
public static final int MAX_VALUE = 0x7fffffff
Constructors
public Integer( int value )
public Integer( String str ) throws NumberFormatException
Constructs and Integer object based on the passed int value or decoded String.
Number Methods
public double doubleValue()
public float floatValue()
public int intValue()
public long longValue()
Returns the internal int value of the Integer object as the indicated primitive data type.
Conversion Methods
public static Integer getInteger( String system-property )
Returns an Integer object containing the int value corresponding to the requested
system-property.
try
{ Integer k = Integer.valueOf( “1998“ );
}
catch ( NumberFormatException e)
{ System.out.println( “Could not convert string to integer.“ );
}
Constants
public static final double MAX_VALUE = 1.79769313486231570e+308
public static final double MIN_VALUE = longBitsToDouble(1L)
public static final double POSITIVE_INFINITY = 1.0 / 0.0
public static final double NEGATIVE_INFINITY = -1.0 / 0.0
public static final double NaN = 0.0d / 0.0
Constructors
public Double( double value )
public Double ( String str ) throws NumberFormatException
Constructs and Double object based on the passed double value or decoded String.
Number Methods
public double doubleValue()
public float floatValue()
public int intValue()
public long longValue()
Returns the internal double value of the Double object as the indicated primitive data
type.
try
{ Double d = Double.valueOf( “3.14e6“ ); }
catch ( NumberFormatException e)
{ System.out.println( “Could not convert string to double.“ ); }
References: Ken Arnold and James Gosling, The Java Programming Language, Addison-Wesley Publishing Company
(1996).
Patrick Chan and Rosanna Lee, The Java Class Libraries, An Annotated Reference, Addison-Wesley
Publishing Company (1997).