Wrapper Classes in Java
Each of the primitives (remember,
String is an object rather than a primitive) has a corresponding class that provides several useful abilities that are otherwise unavailable to a primitive. You can think of the wrapper classes as each being a package of useful abilities wrapped around a primitive of the corresponding type.
Purpose of Wrapper Classes
The wrapper classes in the Java API serve two primary purposes:
- To provide 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.
- To provide a variety of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from
Stringobjects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.
Objects of these wrapper classes are immutable. That is, their values cannot be changed.
An Overview of the Wrapper Classes
There is a wrapper class for every primitive in Java. For instance, the wrapper class for
Integer, the class for
Float, and so on. Remember that the primitive name is simply the lowercase name of the wrapper except for
char, which maps to
int, which maps to
|Primitive||Wrapper classes||Constructor arguments|
|boolean||Boolean||boolean or String|
|byte||Byte||byte or String|
|double||Double||double or String|
|float||Float||float, double or String|
|int||Integer||int or String|
|long||Long||long or String|
|short||Short||short or String|
Autoboxing is the automatic conversion of primitive data types into their corresponding wrapper classes. This is performed as needed so as to eliminate the need to perform trivial, explicit conversion between primitive data types and their corresponding wrapper classes.
Unboxing refers to the automatic conversion of a wrapper object to its equivalent primitive data type. In effect, primitive data types are treated as if they are objects in most situations
Before Java 5, if you wanted to make a wrapper, unwrap it, use it, and then rewrap it, you might do something like this:
Integer y = new Integer(10); // make it int x = y.intValue(); // unwrap it x++; // use it y = new Integer(x); //re-wrap it System.out.println("y = " + y); // print
Now, you can write:
Integer y = new Integer(10); y++; System.out.println("y = " + y);
There are a few things to remember when working with primitives and objects. First, objects can be null, whereas primitives cannot be assigned a null value. This can present problems from time to time. For example, unboxing a null object will result in a
Also, be careful when comparing primitives and objects when boxing does not occur
|Comparison||Two primitives||Two objects||One of each|
|a = = b||Simple comparison||Compare references values||Treated as two primitives|
|a.equals(b)||Will not compile||Compare for equality of values||Will not compile if a is a primitive, otherwise their values are compared.|
How to create Wrapper Objects
The Wrapper Constructors
All of the wrapper classes except
Character provide two constructors: one that takes a primitive of the type being constructed, and one that takes a
String representation of the type being constructed. For instance
Integer i1 = new Integer(10); Integer i2 = new Integer("10"); Float f1 = new Float(3.14f); Float f2 = new Float("3.14f"); Character c1 = new Character('c'); Boolean b = new Boolean("false");
The valueOf() Methods
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 instance,
Integer i2 = Integer.valueOf("101011", 2); //convert 101011 to 43 Float f2 = Float.valueOf("3.14f");
Wrapper Conversion Utilities
When you need to convert the value of a wrapped numeric to a primitive, use one of the many
xxxValue() methods. All of the methods in this family are no-arg methods. Each of the six numeric wrapper classes has six methods, so that any numeric wrapper can be converted to any primitive numeric type. For instance,
Integer i2 = new Integer(10); byte b = i2.byteValue(); short s = i2.shortValue(); double d = i2.doubleValue();
parseXxx() and valueOf()
parseXxx() methods are closely related to the
valueOf() method that exists in all of the numeric wrapper classes can convert
String objects from different bases (radix), when the underlying primitive type is any of the four integer types.
parseXxx()returns the named primitive.
valueOf()returns a newly created wrapped object of the type that invoked the method.
double d4 = Double.parseDouble("3.15"); System.out.println("d4 = " + d4); Double d5 = Double.valueOf("3.15"); System.out.println(d5 instanceof Double);
toXxxString() (Binary, Hexadecimal, Octal)
Long wrapper classes let you convert numbers in base 10 to other bases. These conversion methods,
toXxxString(), take an
long, and return a
String representation of the converted number, for example,
String s3 = Integer.toHexString(254); System.out.println("254 is " + s3);
In summary, the essential method signatures for Wrapper conversion methods are
xxxValue()– to convert a Wrapper to a primitive
parseXxx(String)– to convert a
Stringto a primitive
valueOf(String)– to convert a
Stringto a Wrapper
Advantages of Wrapper classes
- Inclusion of wrapper classes in
nullvalue possibility with Wrapper classes.
- It can be handy to initialize
nullparameters to a method or constructor to indicate state or function. This can’t be possible with primitives.
- We can treat wrapper classes generically / polymorphically as an object along with other objects.
Download this sample code here.