Vous êtes sur la page 1sur 9

WRAPPER CLASSES

Type Wrappers
Primitive types, rather than objects, are used for these quantities for the sake of performance. The primitive types are not part of the object hierarchy, and they do not inherit Object. Java provides type wrappers, which are classes that encapsulate a primitive type within an object. The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean, which are packaged in java.lang. All of the numeric type wrappers inherit the abstract class Number. Number declares methods that return the value of an object in each of the different numeric types.

Numeric Wrappers
Byte
Byte(byte num) Byte(String str) throws NumberFormatException

Short
Short(short num) Short(String str) throws NumberFormatException

Integer
Integer(int num) Integer(String str) throws NumberFormatException

Long
Long(long num) Long(String str) throws NumberFormatException

Float
Float(double num) Float(float num) Float(String str) throws NumberFormatException

Double
Double(double num) Double(String str) throws NumberFormatException

Constants: MIN_VALUE - Minimum value MAX_VALUE - Maximum value

Converting object to Primitive Values


The following methods will convert the invoking object in to the primitive types. All these methods are overridden methods from Number class.
byte byteValue() double doubleValue() float floatValue() int intValue() long longValue() short shortValue()

Converting Numbers to/from Strings


Following methods will convert String object to numeric types.
parseByte() parseShort() parseInt() parseLong()...

Following methods will convert numeric values to String object.


toString() toBinaryString() toHexString() toOctalString()...

Converting String objects to Numeric objects


The following methods will convert String objects in to numeric objects.
static static static static Short valueOf(String str) Integer valueOf(String str) Float valueOf(String str) Long valueOf(String str)...

All these methods may throw NumberFormatException Some other special methods
public static boolean isInfinite(Float) public static boolean isNaN(Double)

Character Wrapper
The constructor for Character is
Character(char ch)

Some methods in this class are


static static static static static static static static static boolean isDigit(char ch) boolean isLetter(char ch) boolean isLetterOrDigit(char ch) boolean isLowerCase(char ch) boolean isUpperCase(char ch) boolean isWhitespace(char ch) char toLowerCase(char ch) char toUpperCase(char ch) char toTitleCase(char ch)

Boolean Wrapper
Boolean is a very thin wrapper It contains the constants TRUE and FALSE Boolean defines these constructors:
Boolean(boolean boolValue) Boolean(String boolString)

This class has some methods like


toString(), equals(), booleanValue() and valueOf()

Vous aimerez peut-être aussi