What is Autoboxing and Unboxing in Java with Example Program

1. Objective

After studying Java Annotations, we move our learning towards what is autoboxing and unboxing in Java. In Addition, we will discuss some advantages & examples of autoboxing and unboxing in Java Programming.

So, let’s start Autoboxing and Unboxing in Java.

Autoboxing and Unboxing in Java

What is Autoboxing and Unboxing in Java with Example Program

2. What is Java Autoboxing and Unboxing

  • Autoboxing in Java: Converting a primitive datatype into a question of the relating wrapper class is called autoboxing in Java. For instance, changing over int to Integer class. The Java compiler applies autoboxing when a crude esteem is:

Gone as a parameter to a strategy that expects a question of the comparing wrapper class. Allotted to a variable of the relating wrapper class.

  • Java Unboxing: Converting a question of a wrapper compose to its relating primitive datatype is called unpacking or Java Unboxing. For instance transformation of Integer to int. Gone as a parameter to a technique that expects an estimation of the comparing primitive datatype. Allocated to a variable of the comparing primitive datatype.

Do you know What is Interface in Java?

3. Autoboxing and Unboxing Examples

a. Example of autoboxing and unboxing in Java

i. Autoboxing and Unboxing Example-

import java.io.*;
class GFG
{
    public static void main (String[] args)
     {
        Integer i = new Integer(10);
        int i1 = i;
        System.out.println("Value of i: " + i);
        System.out.println("Value of i1: " + i1);
        Character gfg = 'a';
        char ch = gfg;
        System.out.println("Value of ch: " + ch);
        System.out.println("Value of gfg: " + gfg);
     }
}

Output – Value of i: 10
The Value of i1: 10
Value of ch: a
Value of gfg:a

Let’s Learn an Important Difference Between Abstract Class and Interface in Java

ii. Autoboxing and Unboxing Example –

import java.io.*;
import java.util.*;
class GFG
{
    public static void main (String[] args)
    {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++)
            list.add(i);
    }
}

In above Example, we have made a rundown of components of Integer write. We are including int primitive types rather than Integer Object and the code effectively aggregated. It doesn’t produce a gather time error as java compiler make Integer wrapper Object from crude int I and adds it to the rundown.

iii. Autoboxing and Unboxing Example –

import java.io.*;
import java.util.*;
class GFG
{
    public static void main (String[] args)
    {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++)
            list.add(Integer.valueOf(i));
    }
}

Read About A Complete Tutorial on Multithreading in Java

iv. Autoboxing and Unboxing Example –

Another case of autoboxing and unboxing in java is to discover aggregate of odd numbers in a rundown. Vital point in the program is that the operators remainder (%) and unary plus (+=) operators don’t have any significant bearing to Integer objects. Yet at the same time code gathers effectively in light of the fact that the unpacking of Integer Object to crude int esteem is occurring by conjuring intValue() strategy at runtime.

import java.io.*;
import java.util.*;
class GFG
{
    public static int sumOfOddNumber(List<Integer> list)
    {
        int sum = 0;
        for (Integer i : list)
        {
            if(i % 2 != 0)
                sum += i;
               using intvalue implicitly
            if(i.intValue() % 2 != 0)
                sum += i.intValue();*/
        }
        return sum;
    }
   public static void main (String[] args)
    {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++)
            list.add(i);
        int sumOdd = sumOfOddNumber(list);
        System.out.println("Sum of odd numbers = " + sumOdd);
    }
}

Output –
Sum of odd numbers = 25

Do You Know What is Garbage Collection in Java – Java GC Collect

Java Quiz

4. Advantages of Autoboxing and Unboxing in Java

  • It helps in writing a clearer note.
  • It helps us use the wrapper class and primitive types interchangeably without the need of typecasting.

This was all about Autoboxing and Unboxing in Java Tutorial. Hope you like our explanation.

5. Conclusion

In this tutorial, we learned what is autoboxing in Java and Java unboxing. Moreover, we saw a various example of Autoboxing and Unboxing in Java. In conclusion, we discussed the advantages of Java Autoboxing and Unboxing. Furthermore, if you have any query feel free to ask in a comment section.

Related Topic- The Island of Isolation in Java 

For reference 

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.