Wrapper Classes
Java provides wrapper classes for each of the primitive types. The
wrapper classes can be used to create objects from the primitive
types. These are the wrapper classes for the corresponding primitive
types:
- Boolean - boolean
- Byte - byte
- Character - char
- Short - short
- Integer - int
- Long - long
- Float - float
- Double - double
Boolean
The Boolean class wraps a value of the primitive type boolean in an
object.
// Creating a Boolean object
boolean found = true;
Boolean boolObj = Boolean.valueOf ( found );
// Getting back a boolean from a Boolean
boolean aBool = boolObj.booleanValue();
Other methods:
- equals ( Boolean obj ) : boolean - Returns true if the Boolean object
represents the same boolean value as this object.
- toString () : String - Returns a string representation of this
Boolean's value.
Character
The Character class wraps a value of the primitive type char in an object.
// Creating a Character object
char ch = 'a';
Character charObj = new Character ( ch );
// Getting a char back from a Character object
char ch2 = charObj.charValue();
Other methods:
- compareTo ( Character charObj ) : int - Returns the integer difference
between the Unicodes of this character and the other character.
- equals ( Character charObj ) : boolean - Returns a boolean true if the
two Character objects have the same value.
- isDigit ( char ch ) : boolean - Determines if the specified character is
a digit.
- isLetter ( char ch ) : boolean - Determines if the specified is a letter.
- isLetterOrDigit ( char ch ) : boolean - Determines if the specified
character is a letter or digit.
- isLowerCase ( char ch ) : boolean - Determines if the specified character
is a lowercase character.
- isUpperCase ( char ch ) : boolean - Determines if the specified
character is an uppercase character.
- toLowerCase ( char ch ) : char - Converts the character to lowercase.
- toUpperCase ( char ch ) : char - Converts the character to uppercase.
- toString() : String - Returns a string representing this character's
value.
Integer
The Integer class wraps a value of the primitive type int in an object.
// Creating an Integer Object
int x = 10;
Integer xObj = new Integer ( x );
// Get the integer value from an Integer object
int z = xObj.intValue();
Other methods
- compareTo ( Integer intObj ) : int - Returns 0 if this Integer is
equal to the value of intObj, a value less than 0 if this Integer is
less than intObj and a value greater than 0 if this Integer is greater
than the value of intObj.
- equals (Integer intObj ) : boolean - Returns true if the values
of the two Integer objects are the same.
- parseInt (String s ) : int - Returns the integer value of the
string.
- toBinaryString ( int i ) : String - Returns a string representation
of the integer in base 2.
- toHexString ( int i ) : String - Returns a string representation
of the integer in base 16.
- toOctalString ( int i ) : String - Returns a string representation
of the integer in base 8.
- toString () : String - Returns a string representation of the
integer.
- valueOf ( String s ) : Integer - Returns an Integer object holding
the value of the specified string.
Double
The Double class wraps a value of the primitive type double in an
object.
// Creating a Double object
double w = 2.512;
Double dObj = new Double ( w );
// Extracting the double value from a Double object.
double v = dObj.doubleValue();
Other Methods
- compareTo ( Double d ) : int - Returns 0 if this Double is numerically
equal to d, a value less than 0 if this Double is numerically less than d,
and a value greater than 0 if this Double is numerically greater than d.
- equals ( Double d ) : boolean - Returns true if the two doubles have
the same value.
- parseDouble ( String s ) : double - Returns a double value represented
by the string.
- toString() : String - Returns a string representation of this Double
object.
- valueOf ( String s ) : Double - Returns a Double object holding the
double value represented by the string.
Vector
The Vector class implements a growable array of objects. One can access the
components using an integer index. But the size of the Vector can grow or
shrink. Each Vector object has a capacity and a capacityIncrement. The
size of the Vector increases in units of the capacityIncrement.
// Creating a default Vector of capacity 10 and capacityIncrement 0
Vector aVec = new Vector();
// Add object at a specified position in the Vector
aVec.add ( 5, anObj );
// Add object at the end of the Vector
aVec.add ( anObj );
// Remove all the elements in the Vector
aVec.clear();
// Return an object at a specified location
Object anObj = aVec.elementAt ( 5 );
// Test if an object is in the Vector
boolean found = aVec.contains ( anObj );
// Get the index of the first occurence of an element
int x = aVec.indexOf ( anObj );
// Get the index of the last occurence of an element
int x = aVec.lastIndexOf ( anObj );
// Remove an element at a specified position
Object anObj = aVec.remove ( 5 );
// Remove the first occurence of an object
boolean done = aVec.remove ( anObj );
// Replace an element at a specified position in a Vector
Object prevObj = aVec.set ( 5, anObj );
// Get the capacity of the Vector
int capa = aVec.capacity();
// Get the number of elements in a Vector
int num = aVec.size();
// Convert a Vector to an Array
Object[] anArray = aVec.toArray();
// Get a string representation of a Vector containing the String
// representation of each element
String str = aVec.toString();