Wrapper classes in java with easy explanation.

Wrapper class  is a mechanism which provides the facility to convert primitive data type into the object and object into primitive data type.

Java 5 provides the two features:

1.Autoboxing

2. Unboxing

1.Autoboxing

Automatic conversion of  primitive data type into an object then this process is known as autoboxing.

2.Unboxing

Automatic conversion of  an object into primitive data type then this process is known as Unboxing.

Why need wrapper class in java

1.It converts primitive data type into an  object and vice-versa.

2.All the Collection Frameworks classes such as ArrayList,LinkedList,HashSet etc only works with objects.

3. The java.util.package provides the facility to work with objects.

4.Primitive can not be null but wrapper class may be null.

5.It can be used to achieve polymorphism.  

Primitive data types equivalent with the wrapper class 


Primitive data type                                                              


Wrapper class


boolean

Boolean

byte

Byte

short

Short

int

Integer

float

Float

double

Double

long

Long

char

Character

Example of Autoboxing

Autoboxing is an automatic process which  converts the primitive data type into equivalent wrapper class.

In java 5,We do not required to use valueOf( ) method to convert primitive data type into  objects.

Example:

Int to Integer,byte to Byte etc

public class AutoboxingWrapperClass
{
public static void main(String[ ] args)
{
int a=10;
Integer i=Integer.valueOf(a); //converting int into Integer

Integer k=a; //Automatic  converting int into Integer

System.out.println(k);
}
}

Output:

10

Example of Unboxing

Unboxing is an automatic process which  converts the wrapper class into equivalent primitive data type.

Example:

Integer to int,Byte to byte etc

public class UnboxingWrapperClass
{
public static void main(String[ ] args)
{
Integer i=new Integer(10); 

int k=i; //Automatic  converting Integer into int

System.out.println(k);
}
}

Output:

10

Example of wrapper class:

Here we will try to understand the example of both autoboxing and unboxing of the wrapper class.

public class WrapperClass
{
public static void main(String[ ] args)
{

//integer data type
int x=10;
//converting int to Integer(primitive into object)
Integer i_obj=new Integer(x);

//byte data type
byte b=20;
//converting byte into Byte

Byte b_obj=new Byte(b);
//Printing the value of primitive data type  into  object
System.out.println(“Wrapper to objects values are given:”);
System.out.print(“Integer primitive to object=”+i_obj);
System.out.print(“Byte primitive to object=”+b_obj);


//Now wrapping an objects into the primitive data type

int i_primitive=i_obj;
byte b_primitive=b_obj;


//Printing the primitive data types value

System.out.println(“objects to wrapper values are given:”);
System.out.print(“Integer obj to primitive =”+i_primitive);
System.out.print(“Byte obj to primitive=”+b_primitive);
}
}

Output:

Wrapper to objects values are given:

Integer primitive to object=10

Byte primitive to object=20

objects to wrapper values are given:

Integer obj to primitive=10

Byte obj to primitive=20

Hope,I explained all the things about the wrapper class in java step by step with easy explanation.If you still have  any query comment me.

Thanks and Regards

Placement preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *