What is autoboxing?

Autoboxing is the automatic conversion between primitive types and reference types. It eliminates the need to explicitely create wrapper objects for primitive types.

Main.java:

public class Main
{
   public static void main(String []args) {
      Integer n1 = 20;   // no need to "new Integer", is done automatically
   
      System.out.println(n1);
      
      int n2 = n1;
       // no need to do "n1.intValue()"
 
      System.out.println(n2);
   }
}

This means that you don’t need to create wrapper objects anymore to use primitives in collection classes.

Main.java:

import java.util.*;
 
public class Main
{
   public static void main(String []args) {
      // allow only integers to be added to the arraylist
      ArrayList<Integer> arrayList = new ArrayList<Integer>();
      
      arrayList.add(10);
      arrayList.add(20);
      arrayList.add(30);
   
      for (int i=0; i<arrayList.size(); i++) {
         System.out.println(arrayList.get(i));
      }
   }
}