|
|
The java.lang Package Certification - Wrapper Classes
- one for each primitive type: Boolean, Byte, Character, Double, Float, Integer, Long, and Short
- Byte, Double, Float, Integer and Short extend the abstract Number class
- all are public final ie cannot be extended
- get around limitations of primitive types
- allow objects to be created from primitive types
- all the classes have two constructor forms
- a constructor that takes the primitive type and creates an object eg Character(char), Integer(int)
- a constructor that converts a String into an object eg Integer("1"). Throws a NumberFormatException if the String cannot be converted to a number
| Note |
- The Character class does not have a constructor that takes a String argument
|
- all, except Character, have a valueOf(String s) method which is equivalent to new Type(String s)
- all have a typeValue() method which returns the value of the object as it's primitive type. These are all abstract methods defined in Number and overridden in each class
- public byte byteValue()
- public short shortValue()
- public int intValue()
- public long longValue()
- public float floatValue()
- public double doubleValue()
- all the classes override equals(), hashCode()
and toString() in Object
- equals() returns true if the values of the compared objects are the same
- hashCode() returns the same hashcode for objects of the same type having the same value
- toString() returns the string representation of the objects value
- all have a public static final TYPE field which is the Class object for that primitive type
- all have two static fields MIN_VALUE and MAX_VALUE for the minimum and maximum values that can be held by the type
Void
- there is also a wrapper class for Void which cannot be instantiated.
| Note |
- The constructors and methods described above do NOT exist for the Void class although it does have the TYPE field.
|
Character
- contains two methods for returning the numeric value of a character in the various number systems
- public static int digit(char ch, int radix)
- public static int getNumber(char ch)
- and one method to return the character value of a number
- public static char forDigit(int digit, int radix)
- has two case conversion methods
- public static char toLowerCase(char ch)
- public static char toUpperCase(char ch)
- also contains a variety of other methods to test wether a character is of a specific type eg isLetter(), isDefined(), isSpaceChar(), etc
- getType() returns an int that defines a character's Unicode type
Integer, Short, Byte and Long
- all have parseType methods eg parseInt(), parseShort, etc that take a String and parse it into the appropriate type
- the Integer and Long classes also have the static methods toBinaryString(), toOctalString() and toHexString() which take an integer value and convert it to the appropriate String representation
Float and Double
- both classes have static fields which define POSITIVE_INFINITY, NEGATIVE_INFINITY, and NaN
- and the following methods to test a value
- public boolean isNan()
- public static boolean isNaN(type value)
- public boolean isInfinite()
- public static boolean isInfinite(type value)
- Float also has a constructor that takes a double value
- both classes have methods to convert a value into a bit pattern or vice versa
- public static int floatToIntBits(float value)
- public static float intBitsToFloat(int bits)
- public static long doubleToLongBits(double value)
- public static double longBitsToDouble(long bits)
|