Wrapper Class in Java
Table of Content:
As you know, Java uses eight primitive types (also called simple types), such as boolean, char, byte, short, int, long, float, double to hold the basic data types supported by the language.
Example
boolean = true ; int i = 5000; float gpa = 13.65f; double rr = 54.44; etc.
However, in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper classes.
Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.
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. Each is briefly examined next.
The above eight classes of java.lang package are known as wrapper classes in java. The list of eight wrapper classes are given below:
Primitive Type | Wrapper class |
---|---|
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
autoboxing and unboxing feature converts primitive into an object and object into primitive automatically. The automatic conversion of primitive into an object is known as autoboxing and vice-versa unboxing.
The object of the wrapper class contains or wraps its respective primitive data type. Converting primitive data types into object is called boxing, and this is taken care by the compiler. Therefore, while using a wrapper class you just need to pass the value of the primitive data type to the constructor of the Wrapper class.
Character is a wrapper around a char. The constructor for Character is
Character(char ch)
Autoboxing
Beginning with JDK 5, Java added two important features: autoboxing and auto-unboxing. Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed. There is no need to explicitly construct an object.
Auto-unboxing is the process by which the value of a boxed
object is automatically extracted (unboxed) from a type wrapper when its value is needed.
There is no need to call a method such as intValue( )
or doubleValue( )
.
Integer marks = 100; // autobox an int
Here is the modern way to construct an Integer object that has the value 100. Notice that no object is explicitly created through the use of new. Java handles this for us, automatically.
Unboxing
To unbox an object, simply assign that object reference to a primitive-type variable. For example, to unbox iOb, you can use this line:
int i = marks; // auto-unbox
Here is the preceding program rewritten to use autoboxing/unboxing:
Program:
// Demonstrate autoboxing/unboxing. class AutoBox { public static void main(String args[]) { Integer marks = 100; // autobox an int int i = marks; // auto-unbox System.out.println(i + " " + marks); // displays 100 100 } }
Program:
100 100 Press any key to continue . . .
Primitive to Wrapper conversion
Program:
public class WrapperExample1{ public static void main(String args[]){ //Converting int into Integer int a=15; Integer i=Integer.valueOf(a);//converting int into Integer //autoboxing, now compiler will write Integer.valueOf(a) internally Integer k=a; System.out.println(a+" "+i+" "+k); } }
Program:
15 15 15 Press any key to continue . . .
Wrapper to Primitive conversion
Program:
public class WrapperExample1{ public static void main(String args[]){ //Converting Integer to int Integer a=new Integer(15); int i=a.intValue();//converting Integer to int //unboxing, now compiler will write a.intValue() internally int j=a; System.out.println(a+" "+i+" "+j); } }
Program:
15 15 15 Press any key to continue . . .
Example of boxing and unboxing ?
Program:
public class wrapper { public static void main(String args[]) { Integer s = 12; // boxes int to an Integer object s = s + 20; // unboxes the Integer to a int System.out.println(s); } }
Program:
32 Press any key to continue . . .
When s is assigned an integer value, the compiler boxes the integer because s is integer object. Later, s is unboxed so that they can be added as an integer.