Introduction
We learned that java is object oriented language that means java programming is all about objects. Because of primitive data type, we can’t say java is pure object oriented language. Primitive data type is not an object because it is not belongs to any class it is just a normal variable. Because of the wrapper class we can say Java is 100% object oriented language. Let’s discuss here how we can achieve java is a pure object oriented programming by using wrapper class.
What is Wrapper class?
Wrapper class is class its object contains primitive data type. Wrapper class is used to converts primitive data type into corresponding wrapper class objects.
Primitive | Wrapper class |
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
Why wrapper class is required?
- Whenever we work with collection API to provide type safety generics are used. Wrapper classes are used as generics in collections in java. Generics takes only objects but not primitive.
ArrayList<Integer> Al= new ArrayList<>();
Al.add(10);
Al.add(20);
Al.add(“Protech”); //compile error
HashMap<Integer, String> hmObj = new HashMap<Integer, String>();
Here we are restricting that HashMap key should be Interger type and value should be String type.
- Whenever we are fetching data from UI, possibility that value might null. In this scenario we can’t work with primitive data type as primitive variable does not hold null value. Here best to go for wrapper class object as it hold null value too.
What is boxing and unboxing?
Conversion of primitive data type into object is called Boxing. Automatic conversion of primitive data type into object is called Autoboxing. Similarly, conversion of object into primitive data type is called unboxing. Automatic conversion of object into primitive data type is called Autounboxing.
Example1: Boxing and Unboxing
public class WrapperClassSession {
//Boxing and Autoboxing
public static void main(String[] args) {
int number = 100; // Initialization of primitive data type
Integer value1 = new Integer(number); // Initialization of wrapper class object
Integer value2 = new Integer(number); // Initialization of wrapper class object
Integer value3 = Integer.valueOf(number); // Boxing
Integer value4 = 20; //Auto boxing
System.out.println(number);
System.out.println(value1);
System.out.println(value2);
System.out.println(value3);
System.out.println(value4);
}
}
/*
* Output:
100
100
100
100
20
*/
Example2: Unboxing and Autounboxing
public class WrapperClassSession {
//Unboxing and AutoUnboxing
public static void main(String[] args) {
Integer value1 = new Integer(20); // Initialization of wrapper class object
int value2 = value1.intValue(); // Unboxing
int value3 = value1; //Auto unboxing, here compiler will write value1.intValue() internally
System.out.println(value1);
System.out.println(value2);
System.out.println(value3);
}
}
/*
* Output:
20
20
20
*/
Recommended article for you,
Introduction to Selenium (Overview)
Hey, Now it is your time! Drop a comment if more details needed or if any update requires. Your comments are more valuable to improve our site to help others.
Kitabın Paypal Kitap Satış
Hello to all, because I am actually eager of reading this web site’s post to be updated regularly.
It includes nice stuff.