What is a Wrapper Class!??

Wrapper Class:

Wrapper class essentially allows the user to wrap the primitive data into an object this means that the user can use those primitive data in the form of an object this has many uses in Java as it is fully Object Oriented programming.

Uses of Wrapper Class:

  1. We can not use primitive datatypes when working with methods in Java class as they only work on objects so we need to convert the primitive data type.

  2. With the help of wrapper classes, we can convert primitive datatypes into objects of our own choice for example we can convert a String into an Integer and can even do operations on it.

     String numStr = "123";
     int intNum = Integer.parseInt(numStr); // Parsing string to int
    
     double doubleValue = 3.14;
     String doubleStr = Double.toString(doubleValue); // Converting double to string
    

What is Autoboxing?

Automatic conversion of primitive datatype into object is called autoboxing it is done by the compiler itself.

//Java program to convert primitive into objects  
//Autoboxing example of int to Integer  
public class WrapperExample1{  
public static void main(String args[]){  
//Converting int into Integer  
int a=20;  
Integer i=Integer.valueOf(a);//converting int into Integer explicitly  
Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally  

System.out.println(a+" "+i+" "+j);  
}}