In this article we learn the what is wrapper class in java, why we use wrapper class, autoboxing & unboxing in wrapper class and valueOf () and parseInt() method in Wrapper Class.
What is Wrapper Class in Java?
A Wrapper class in Java is the type of class that provides a mechanism to convert the primitive data types into the objects and vice-versa.
When a wrapper class is created, there is a creation of a new field in which we store the primitive data types. The object of the wrapper class wraps or holds its respective primitive data type.
While using a wrapper class, we just have to pass the value of the primitive data type to the constructor of the Wrapper class.
All the wrapper classes Byte, Short, Integer, Long, Double and, Float, are subclasses of the class Number. While Character and Boolean wrapper classes are the subclasses of class Object.
Sr No |
Primitive Data Type |
Wrapper Class |
Constructor Argument |
1 |
boolean |
Boolean |
boolean or String |
2 |
Byte |
Byte |
byte or String |
3 |
char |
Character |
char |
4 |
Int |
Integer |
Int or String |
5 |
Float |
Float |
float or String |
6 |
Double |
Double |
double or String |
7 |
Long |
Long |
long or String |
8 |
Short |
Short |
short or String |
Why we use wrapper class in java?
As the wrapper classes have objects, we can store null as a value. We could not store null in variables of primitive datatype.
The Collection Framework in Java works only with objects. All classes of the collection framework like Array List, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, work only with objects.
If we pass a primitive value using call by value, it will not change the original value. But it will change the original value if we convert the primitive value into an object.
Autoboxing and Unboxing in Wrapper Class
Autoboxing is the process of converting a primitive datatype to its corresponding Wrapper class.
Unboxing is the inverse of Autoboxing. It is the process of converting a Wrapper class object into its corresponding Primitive Datatype.
For example: an Integer object would be converted to a primitive data type, i.e., int.
Program 1:
public class Ex1 {
public static void main (String [] args) {
int a=10;
Integer intobj1=new Integer (10); // Boxing
Integer intobj2=new Integer (a); // Boxing
int b=intobj1.intValue(); // Un-Boxing
System.out.println(“a =”+a);
System.out.println(“intobj1 =”+intobj1);
System.out.println(“intobj2 =”+intobj2);
System.out.println(“b =”+b);
Output:
a =10
intobj1 =10
intobj2 =10
b =10
valueOf () and parseInt() method in Wrapper Class
valueOf ()-Return a Wrapper class object holding the specified primitive type value.
parseInt ()-Retrieves the primitive data type of a specified String.
Program:
public class StringToInteger {
public static void main (String [] args) {
String Str1=”5000″;
//method -1
Integer intobj=Integer.valueOf(Str1); // Convert String to int
int a= intobj.
System.out.println(“a =”+a);
//method -2
int i=Integer.parseInt(Str1); // Convert String to int
double d=Double.parseDouble(Str1) ; // Convert String to double
short S1=Short.parseShort(Str1); // Convert String to short
System.out.println(“i =”+i);
System.out.println(“d =”+d);
System.out.println(“S1 =”+S1);
// int to String convert
String str2=String. ValueOf(i);
System.out.println(“I=”+str2);
}
}
Output:
a =5000
i =5000
d =5000.0
S1 =5000
I=5000