Wrapper Class in Java

Java is Object Oriented Programming language and can be view everything as an object i.e. a file can be treated as an object,  image can be treated as an object and similarly primitive data type which are not object  can be treated as an object (with Wrapper class).
In simple way we can define
Wrapper class is a mechanism to convert primitive data type into an object and also an object in to primitive data type.
for example :Suppose num1 is an ineteger variable like this

int num1;

we want to create an object of num1 then with help of wrapper class it is possible i.e. we can create an object of  num1 of its corresponding wrapper class Integer like this

Integer n1 =new Integer(10);
num1=n1;

Note  : java.lang package contains Wrapper classes
The eight classes of java.lang package are known as wrapper classes in java, following table
Contains Primitive data and  its corresponding wrapper class.
Primitive Data Type Wrapper Class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Auto Boxing and UnBoxing
Whenever we talk about Wrapper class then a question arises in our mind.
 Is it possible that Java compiler automatically converts primitive data types into object and vice versa?
Then answer is yes Java version 1.5 and onwards it is possible that Java compiler automatically converts primitive data type into object and object in to primitive data type, and technical terms used for this operation is AutoBoxing and UnBoxing.
 AutoBoxing : - Autoboxing is the automatic conversion of primitive data type to object i.e. it automatically converts primitive data type to its corresponding Wrapper class.
Simplest example of AutoBoxing :


public class AutoBoxingExample {

    public static void main(String a[]){

      Integer autoint=100;

      System.out.println(autoint);

    }   
} 

OutPut :
When AutoBoxing occured ?

AS we discussed previously AutoBoxing is process of automatically conversion of primitive data type to object and AutoBoxing occurred  in these following situations :
  • Passed as a parameter to a method that expects an object of the corresponding wrapper class·       
  • Assigned to a variable of the corresponding wrapper class.

Passed as a parameter to a method that expects an object of the corresponding wrapper class .
Example:

public class AutoBoxing2 {

    public static void checkMethod(Integer num){   //pass as an Argument

        System.out.println(num);

    }
    public static void main(String a[]){

        checkMethod(2); 

    }  

}



OutPut :
Assigned to a variable of the corresponding wrapper class.


public class AutoBoxingExample {

    public static void main(String a[]){  

      Integer autoint=100;

      System.out.println(autoint);

    }  
}
 

OutPut :

UnBoxing : UnBoxing is the process of automatically conversion of  object to primitive data type.
When UnBoxing Occurred ?

UnBoxing  is process of automatically conversion of object  of wrapper type to its primitive data type.
The Java compiler applies unboxing when an object of a wrapper class is.
  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Example :

public class UnBoxingExample {

    static void checkMyMethod(Integer num){ 

     System.out.println(num);

    }

    public static void main(String a[]){ 

        Integer numVal=new Integer(100);

        checkMyMethod(numVal);

    } 
}


Comments

Popular posts from this blog

Secure Database Connectivity in node.js with mysql

Export data from mysql db to csv file using java

API (Application Programming Interface)