- Home
- Objectives

- XyzWs Study Guides
- Study Guides
- Study Notes
- Resources

- Mock Exams
SCJP Study Guide:
API Contents
Printer-friendly version |
Mail this to a friend
Wrapper Classes
The java.lang package provides standard library classes, called wrappers,
that are closely related to primitives. The wrappers are classes
that wrap up primitive values in classes that offer utility methods to
manipulate the values. For each primitive type (boolean, byte, char, double,
float, int, long, and short), there is one wrapper class corresponding to
it (Boolean, Byte, Character, Double, Float, Integer, Long, and Short).
For example, the Character class wraps a value of the primitive
type char in an object. An object of type Character contains
a single field whose type is char.
All wrapper classes are public final and hence cannot be sub classed. All wrapper objects are immutable. Once an object is created, the wrapped primitive value cannot be changed.
The wrapper classes can take constructors of either the type they are designed to wrap or of a String that can be converted to that type. One exception, Character wrapper class does not have a constructor that takes a String argument. Most of these constructor throws NumberFormatException, which is a runtime exception.
The most methods of the wrapper classes are static so you can use them without creating an instance of the matching wrapper class.
The wrapper classes also offer utility methods for converting to and from the primitive values they represent.
- The static valueOf(..) method returns a wrapper instance representing the specified primitive value. For example, Character.valueOf(char c) returns a Character instance representing the specified char value. Most wrapper classes provide two forms of valueOf(..) method: one takes its primitive value and one takes a string except Character class that does not take a string.
-
Each of wrapper class has a typeValue() method which returns the primitive
value of that wrapper instance. For example, myCharacter.charValue() returns
the value of this
myCharacteras a char. -
Each wrapper class, except Character, has a corresponding parseType methods that turn a String that can represent a number into the primitive version of that number. The use of Type is to stand in for any of the data types that the wrappers can contain, thus it includes parseInt, parseLong, parseShort, parseCharacter, parseBoolean.
Wrapper classes override override equals(), hashCode() and toString() in Object .
-
The equals() on wrappers return
trueif and only if the argument is notnulland is a same wrapper object that contains the sameprimitivevalue as this object. The equals() on wrappers return false if both the objects are not instances of the same class. This statement is true even when the value they are wrapping has the same numerical value. - The hashCode() returns the same hashcode for objects of the same type having the same value.
- The toString() returns the string representation of the objects value.
Byte, Double, Float, Integer, Long, and Short
The abstract class Number is the superclass of classes BigDecimal,
BigInteger, Byte, Double, Float,
Integer, Long, and Short
.
Subclasses of Number must provide methods to convert the
represented numeric value to byte, double, float,
int, long, and short.
-
public byte byteValue() returns the value of the specified number as a
byte -
public short shortValue() returns the value of the specified number as a
short -
public int intValue() returns the value of the specified number as a
int -
public long longValue() returns the value of the specified number as a
long - public float floatValue() returns the value of the specified number as a float
-
public double doubleValue() returns the value of the specified number as a
double
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. To understand what these methods are likely to return you need to have a basic understanding of binary and hex number representation. You will need to understand those concept for the objectives relating to bit shifting.
Float & 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)
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) returns Lowercase version of c
- public static char toUpperCase(char ch) returns Uppercase version of c
-
contains a variety of static methods to test wether a character is of a
specific type
-
returns true if c is digit character.public static booleanisDigit(c) -
returns true if c is letter character.public static booleanisLetter(c) -
returns true if c is letter or digit.public static booleanisLetterOrDigit(c) -
returns true if c is lowercase char.public static booleanisLowerCase(c) -
returns true if c is uppercase char.public static booleanisUpperCase(c) -
returns true if c is space, tab, ....public static booleanisWhitespace(c)
-
-
getType()returns anintthat defines a character's Unicode type