Introduction
Java is an object-oriented language and can view everything as an
object. A simple file can be treated as an object (with java.io.File), an address of a system
can be seen as an object (with java.util.URL),
an image can be treated as an object (with java.awt.Image) and a simple data type can be converted into an
object (with wrapper classes).
This tutorial discusses wrapper classes. Wrapper classes are used to convert any data type into an
object.
The primitive data types are not objects; they do not belong to
any class; they are defined in the language itself. Sometimes, it is required
to convert data types into objects in Java language. For example, upto JDK1.4,
the data structures accept only objects to store. A data type is to be converted
into an object and then added to a Stack or Vector etc. For this conversion,
the designers introduced wrapper
classes.
As the name says, a wrapper class wraps (encloses) around a data
type and gives it an object appearance. Wherever, the data type is required as
an object, this object can be used. Wrapper classes include methods to unwrap
the object and give back the data type
This tutorial contains the details about what is
wrapper classes and their use. It contains many different examples and source
code for wrapper classes purpose and application.
Java uses primitive types (also called simple
types), such as int or double, to hold the basic data types supported by the
language.
But many time when you will need an object
representation of primitive types, For example, you can’t pass a primitive type
by reference to a method.
Also, many of the standard data structures
implemented by Java operate on objects, which means that you can’t use primitive
data structures to store primitive types, To handle these kind of situations,
Java provides type wrappers or wrapper classes.
What
is wrapper class in java
In Java, There is a wrapper class for every
primitive datatype.
Basically the Wrapper classes encapsulate a
primitive type within an object.
For example, the wrapper class for int is Integer,
the class for float is Float, class for char is Character and so on.
The type wrappers are Double, Float, Long, Integer,
Short, Byte, Character, and Boolean.
These classes offer a wide array of methods that
allow you to fully integrate the primitive types into Java’s object hierarchy.
Wrapper classes provides a mechanism to
"wrap" primitive values in an object so that the primitives can be
included in activities reserved for objects, like being added to Collections,
or returned from a method with an object return value.
Note that autoboxing feature of Java 5 automatically
does the wrapping operations.
Wrapper classes provide an many utility functions
for primitives, Most of these functions are related to various conversions:
converting primitives to and from String objects, and converting primitives and
String objects to and from different bases (or radix), such as binary, octal
and hexadecimal.
Examples of wrapper classes in java
Character
- Wrapper class
Character is a wrapper around a char. The
constructor for Character is Character(char ch) Here, ch specifies the
character that will be wrapped by the Character object being created.
To obtain the char value contained in a Character
object, call charValue( ), shown here:
char charValue( ) // It returns the encapsulated
character.
Boolean
- Wrapper class
Boolean is a wrapper around boolean values. It
defines these constructors:
Boolean(boolean boolValue)
Boolean(String boolString)
In the first version, boolValue must be either true
or false. In the second version, if boolString contains the string “true” (in
uppercase or lowercase), then the new Boolean object will be true. Otherwise,
it will be false.
To obtain a boolean value from a Boolean object, use
booleanValue( ), shown here:
boolean booleanValue( ) // It returns the boolean
equivalent of the invoking object.???
The
Numeric Type Wrappers
The most commonly used type wrappers are those that
represent numeric values.
These are Byte, Short, Integer, Long, Float, and
Double. 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 number formats. These methods are shown here:
byte
byteValue( )
double
doubleValue( )
float
floatValue( )
int
intValue( )
long
longValue( )
short
shortValue( )
For example, doubleValue( ) returns the value of an
object as a double, floatValue( ) returns the value as a float, and so on.
These methods are implemented by each of the numeric
type wrappers.
All of the numeric type wrappers define constructors
that allow an object to be constructed from a given value, or a string
representation of that value.
For example, here are the constructors defined for
Integer:
Integer(int
num)
Integer(String str)
If str does not contain a valid numeric value, then
a NumberFormatException is thrown.
All of the type wrappers override toString( ). It
returns the human-readable form of the value contained within the wrapper.
// Demonstrate a type wrapper.
class Wrap {
public
static void main(String args[]) {
Integer
iOb = new Integer(100);
int i =
iOb.intValue();
System.out.println(i + " " + iOb); // displays 100 100
}
}
The two (well, usually two) static valueOf() methods
provided in most of the wrapper classes give you another approach to creating
wrapper objects.
Both methods take a String representation of the
appropriate type of primitive as their first argument, the second method (when
provided) takes an additional argument, int radix, which indicates in what base
(for example binary, octal, or hexadecimal) the first argument is represented.
For example,
Integer i2 =
Integer.valueOf("101011", 2); // converts 101011
// to 43 and
assigns the value 43 to the Integer object i2
or
Float f2 =
Float.valueOf("3.14f");
// assigns
3.14 to the Float object f2
Keypoint
- Wrapper class
Wrapper objects are immutable, means that once they
have been given a value, that value cannot be changed.
Autoboxing feature which introduced from Java 5
automatically does the wrapping operations.
Wrapper classes provides many utility functions for
various conversions: converting primitives to and from String objects, and
converting primitives and String objects to and from different bases (or
radix), such as binary, octal and hexadecimal.
No comments:
Post a Comment