0% found this document useful (0 votes)
32 views3 pages

Wrappers Classes

Java provides wrapper classes for the 8 primitive data types in the java.lang package. These classes define object containers to store primitive values and provide methods to convert between primitive types and wrapper objects. All wrapper classes support constructors to create objects from primitive values or strings, as well as methods like equals(), hashCode(), and toString(). The Integer and Double classes demonstrate specific constructors, constants, and conversion methods for integer and floating-point types.

Uploaded by

Anji Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Wrappers Classes

Java provides wrapper classes for the 8 primitive data types in the java.lang package. These classes define object containers to store primitive values and provide methods to convert between primitive types and wrapper objects. All wrapper classes support constructors to create objects from primitive values or strings, as well as methods like equals(), hashCode(), and toString(). The Integer and Double classes demonstrate specific constructors, constants, and conversion methods for integer and floating-point types.

Uploaded by

Anji Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Wrapper Classes Lawrence M.

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.

• 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.

1 December 31, 1998


Wrapper Classes Lawrence M. Brown
[email protected]

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"; • Dot operator, “.” is left


int k1 = Integer.parseInt( number ); associative (left to right).
int k2 = Integer.parseInt( "2458" );

double d1 = Double.valueOf( "31.6" ).doubleValue();


double d2 = Double.valueOf( number ).doubleValue();

Convert String to Double object.

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“ );

2 December 31, 1998


Wrapper Classes Lawrence M. Brown
[email protected]

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).

3 December 31, 1998

You might also like