Вы находитесь на странице: 1из 3

Wrapper Classes

Lawrence M. Brown ktee@erols.com

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. The wrapper classes belong to the java.lang package. Selected source code from the Integer wrapper class:
public final class Integer extends Number { public static final int MIN_VALUE = 0x80000000; public static final int MAX_VALUE = 0x7fffffff; private int value; public Integer( int value ) { this.value = value; } public static Integer valueOf( String s, int radix ) throws NumberFormatException { return new Integer(parseInt(s,radix)); } ... }

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. An equals() method to compare objects for equality. A hashCode() method to compute the has code value for the object. A toString() method that generates the string representation of the wrapper object.

December 31, 1998

Wrapper Classes

Lawrence M. Brown ktee@erols.com

2. Converting Strings to Numbers


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. String number = "-37"; int k1 = Integer.parseInt( number ); int k2 = Integer.parseInt( "2458" ); double d1 = Double.valueOf( "31.6" ).doubleValue(); double d2 = Double.valueOf( number ).doubleValue();
Convert String to Double object. Dot operator, . is left associative (left to right).

Convert Double object to primitive data type.

3. Selected Methods from Integer Class


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. public static int parseInt( String str ) throws NumberFormatException Returns an int containing the numeric value of the decoded integer string. int k = Integer.parseInt( 126 );

December 31, 1998

Wrapper Classes

Lawrence M. Brown ktee@erols.com

public String toString() public static String toString( int num ) Generates a String representation of an int, or Integer object. public static Integer valueOf( String str ) throws NumberFormatException Creates an Integer object based on the decoded String. try { Integer k = Integer.valueOf( 1998 ); } catch ( NumberFormatException e) { System.out.println( Could not convert string to integer. ); }

4. Selected Methods from the Double Class


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. public String toString() public static native String toString( double value ) Generates a String representation of a double value, or Double object. public static native Double valueOf( String str ) throws NumberFormatException Creates a Double object based on the decoded String. 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).

December 31, 1998

Вам также может понравиться