Wrapper Class in Java – Autoboxing and Unboxing tutorial

Introduction

In this article, we will learn wrapper class in Java, autoboxing and unboxing with examples.

As we know, Java is a Object-Oriented-Programming language and thus everything in Java is a object and should be.

However, Java has primitive data types that are not objects.

Java primitive data types – byte, int, long, short, float, double, char and boolean.

Wrapper class in Java is used for converting these primitive data types into objects.

For example : int can be converted to Integer, long can be converted to Long.

In Java version 5, two new features were released : Autoboxing and Unboxing.

Autoboxing and Unboxing automatically converts the primitive type into objects and objects into primitive type respectively.

Let’s learn in details :

List of Wrapper classes in Java

There are eight wrapper classes present in Java. These classes are available under java.lang package.

Below is the list of wrapper classes in java and their respective primitive types :

Wrapper ClassPrimitive Type
Bytebyte
Shortshort
Integerint
Longlong
Floatfloat
Doubledouble
Booleanboolean
Characterchar

What is the use of Wrapper class in Java?

As Java is an OOP based language, there are always requirement of dealing with objects. Many Java concepts require the use of objects only and not primitive types.

Some of the example of those concepts : Collections, Serialization, Synchronization, etc.

There are thousands of scenarios where wrapper class is needed in java.

Let’s look at some of the cases :

1) Changing the value

  • There may be case where the value in method is needed to change but java only supports call by value.
  • Hence, we can not pass a primitive value as it wont change the original value.
  • However, if we convert the primitive type to object then we can change the original value.
  • Thus, here wrapper classes are used.

2) Collection Framework (classes in java.util package)

  • All the classes present in java.util package in java deals with objects only.
  • Meaning, you cannot store the primitive values in any collections framework.
  • So we will need to convert the primitives to objects in order to use collections framework.

3) Serialization

  • Serialization in java requires the object to be converted into streams so we need objects.
  • Here , wrapper classes are used if we have primitives and want to perform serialization.

4) Java Synchronization

  • Synchronization in java also works only with objects and not with primitives.

Autoboxing in Java

Autoboxing is the concept of automatic conversion of primitive data types to objects (respective wrapper classes).

Example : int will get automatically convert to Integer.

Similarly long to Long, byte to Byte, char to Character etc.

Autoboxing is released in Java version 5.

In Earlier versions of java, we were required to use the valueOf() method to convert the primitives into objects. valueOf() is the method of wrapper class.

Example: Wrapper class program of Autoboxing in Java

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

//declaring primitive data type
int value = 123;  

//converting primitive to object manually
Integer value1 = Integer.valueOf(value);

//Autoboxing will happen
Integer value2 = value;
  
System.out.println("Manual conversion result - "+value1);  

System.out.println("Autoboxing conversion result - "+value2);  

}}  

Output:

 Manual conversion result - 123 
 Autoboxing conversion result - 123 

Unboxing in Java

Unboxing is the concept of automatic conversion of wrapper types to primitives. It is the reverse process of Autoboxing.

Example : Integer will get automatically convert to int.

Similarly Byte to byte, Character to char etc.

Unboxing is released in Java version 5.

In Earlier versions of java, we were required to use the intValue() method to convert the objects into primitives.

Example: Wrapper class program of Unboxing in Java

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

//declaring wrapper data type
Integer value = new Integer(123);    
  

//converting wrapper object to primitive manually
int value1 = value.intValue();

//Autoboxing will happen
int value2 = value;
  
System.out.println("Manual conversion result - "+value1);  

System.out.println("Unboxing conversion result - "+value2);  

}} 

Output:

Manual conversion result - 123
Unboxing conversion result - 123
java wrapper classes

Example: Program of Wrapper class in Java

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

boolean boolean_val = true; 
byte byte_val = 10; 
char char_val = 'x';  
int int_val = 20;    
short short_val = 30;  
long long_val = 40;  
float float_val = 50.0F;  
double double_val = 60.0D;  
 
  
//Autoboxing conversion
Boolean boolean_obj = boolean_val;
Byte byte_obj = byte_val;
Character char_obj = char_val;
Integer int_obj = int_val;  
Short short_obj = short_val;
Long long_obj = long_val;
Float float_obj = float_val; 
Double double_obj = double_val;

  
//Printing wrapper objects  
System.out.println("Printing wrapper object values");  

System.out.println("Boolean : "+boolean_obj); 
System.out.println("Byte : "+byte_obj);  
System.out.println("Character : "+char_obj); 
System.out.println("Integer : "+int_obj);  
System.out.println("Short : "+short_obj);  
System.out.println("Long : "+long_obj);  
System.out.println("Float : "+float_obj);  
System.out.println("Double : "+double_obj);  
 
 
}}  

Output:

Printing wrapper object values 
Boolean : true 
Byte : 10 
Character : x 
Integer : 20 
Short : 30 
Long : 40 
Float : 50.0 
Double : 60.0 

How to create custom wrapper class in Java?

In Java, we can create custom wrapper classes. Wrapper classes wraps the primitive into itself. We will create one custom wrapper class in this below example.

Example : Program for Custom wrapper class


class Example{ 
    
private int value;  

Example(){}  

Example(int value){  
this.value = value;  
}  


public int getValue(){  
return value;  
}  


public void setValue(int value){  
this.value = value;  
}  


@Override  
public String toString() {  
  return Integer.toString(value);  
}  

    
}  

 
public class Sample{  
    
public static void main(String[] args){ 
    
Example value1=new Example(25);  

System.out.println("value1 - "+value1);  

}}  

Output:

 value1 - 25 

Further Readings:

Conclusion

Wrapper class in Java, autoboxing , unboxing and custom wrapper classes are very important for java as java language is all about objects and these concepts provide the facility of conversion from primitives to objects.

Thanks for Reading!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply