auto-boxing and unboxing in Java

In this tutorial, we will learn how to do auto-boxing and unboxing in java. Further, we will be discussing the advantages of auto-boxing and unboxing and let us see how the auto-boxing and unboxing works in java
If you guys don’t know how to use auto-boxing and unboxing in java, don’t worry! You are at the right place to learn about this interesting topic in a very easy manner through examples

Introduction to oops concept in Java: why the object-oriented program is useful in java? As we all know that java is an object-oriented programming language(oops) that is, java supports object-oriented concepts. As we all know that java is not purely object-oriented programming language(because it supports primitive data types) sometimes we have to include oops concept in java to fulfil our requirements by creating objects to the data created. We use object-oriented programming because it enables re-usability, easier to maintain software, define abstract data types.

Introduction to wrapper classes in Java: before going into auto-boxing and unboxing let us understand what are wrapper classes. Wrapper classes: As the name itself suggests that wrap means combining and these are known as wrapper classes because they wrap or combine primitive data types into their corresponding object of wrapper classes. Hence, this is a brief explanation of wrapper classes in java. So we get a now clear idea about oops and wrapper classes so far. You might have a question that why are we discussing oops concept and wrapper classes. Don’t worry! Here you will get a detailed explanation about why we have discussed oops concept and wrapper classes in java.

Introduction to auto-boxing and unboxing in Java: 

As we all know the data input in java is expected in two types of data format i.e., primitive data types and reference data types. To convert primitive data types to reference data type and vice-versa, we use the concept of auto-boxing and unboxing in java. As discussed earlier, wrapper classes in java added TWO important features:

  • AUTO-BOXING
  • UNBOXING

Before knowing the above features in detail, let us learn about primitive data types and their corresponding wrapper classes.

Primitive data types: byte, short, int, long, float, double, char, boolean.
Wrapper classes: Byte, Short, Integer, Long, Float, Double, Character, Boolean.

NOTE: From the above, we can say that the wrapper classes are same as primitive data types as they only differ from their naming convention i.e., all the wrapper classes must start with upper-case letters. You guys might get a question that why are we discussing primitive data types and their corresponding wrapper classes. Don’t worry! Here we will discuss in-detail about auto-boxing and unboxing.       

AUTO-BOXING: Auto-boxing is one of the features of wrapper classes. As the name itself suggests that auto-boxing helps in conversion of primitive data types into their corresponding wrapper classes automatically.

simple example 1: 

int a = (you can enter any number of your own choice);
Integer i = a; //auto-boxing using constructor

example 2: 

int i = (you can enter any number of your own choice);
Integer j = Integer.valueof(i); //auto-boxing using value of() method

NOTE: In the above, auto-boxing can be done in two ways i.e, using constructors and method(valueof())

UNBOXING: Unboxing is also one of the features of wrapper classes. This helps in the conversion of an object of the wrapper class into its primitive data type. It is exactly the opposite process of auto-boxing.

a simple example:

Integer obj1 = new Integer (enter number of your choice);
int c = obj1.intvalue();
int a = obj1; // 

unboxing using methods

NOTE: In the above; unboxing is only done by methods.

  • ADVANTAGES OF AUTO-BOXING AND UNBOXING :
    • It helps the developers to write cleaner code and easy to understand
    • It helps us to convert primitive data type into wrapper classes and vice-versa internally, so it is easier for conversion

Now let’s write Java program to understand in-detail

AUTO-BOXING JAVA PROGRAM :

class Wrapper1
{
public static void main(String args[])
    {
         int a = 256;    // user can enter any number
         Integer b = Integer.valueof(a);
         Integer c = a;  // auto-boxing using method valueof()
         System.out.println("value of b:" + b);
         System.out.println("value of c:" + c);
         System.out.println("sum of b & c:"+ b +c);
    }
}

output:

the value of b:256
value of c:256
the sum of b & c: 512

UNBOXING JAVA PROGRAM:

class Wrapper2
{
     public static void main(String args[])
        {
            Integer i = new Integer(300);  // user can enter any number
            int obj1 = i.intvalueof(i);
            int j = i;                    // unboxing using method
            System.out.println("value of j:" + j);
        }
}

output :

value of j: 300

Also read:

How to merge two linked lists in Java

Leave a Reply

Your email address will not be published.