Volodymyr Portianko
Java Engineer at Playtika

Autoboxing and Unboxing in Java

Published in the Java Developer group
In Java, a feature of primitives and their wrappers is autoboxing / unboxing. Let's dig into this concept. As we've already learned, Java is an object-oriented language. That means that all programs written in Java are made of objects. Primitives are not objects. But even so, a wrapper variable can be assigned a primitive value. This process is called autoboxing. Similarly, a primitive variable can be assigned a wrapper object. This process is called unboxing. For example:

public class Main {
   public static void main(String[] args) {
       int x = 7;
       Integer y = 111;
       x = y; // Unboxing
       y = x * 123; // Autoboxing
   }
}
In line 5, we assign y, which is an Integer object, to the primitive x. As you can see, we don't have to take any additional steps: the compiler knows that int and Integer are, essentially, the same thing. That's unboxing. Something similar is happening with autoboxing on line 6: the primitive value (x * 123) is easily assigned to object y. This is an example of autoboxing. That's why the term includes the word "auto": because you don't have to do anything special to assign primitives to their corresponding wrapper objects (and vice versa). It all happens automatically. Convenient, huh? :) We see another example of the convenience of autoboxing / unboxing when working with methods. This is because method arguments are also autoboxed and unboxed. For example, if a method takes two Integer objects as inputs, we can easily pass ordinary ints instead!

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

       printNumber(7);// A standard int, not even an int variable
   }

   public static void printNumber(Integer i) {
       System.out.println("You entered the number " + i);
   }
}
Output:
You entered the number 7
It works in the other direction, too:

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

       printNumber(new Integer(632));
   }

   public static void printNumber(int i) {
       System.out.println("You entered the number " + i);
   }
}
An important point you need to remember is this: autoboxing and unboxing don't work for arrays!

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

       int[] i = {1,2,3,4,5};
      
       printArray(i);// Error, this won't compile!
   }
  
   public static void printArray(Integer[] arr) {
       System.out.println(Arrays.toString(arr));
   }
}
Autoboxing and Unboxing in Java - 1Attempting to pass an array of primitives to a method that takes an array of objects will result in a compilation error. In conclusion, let's briefly compare primitives and wrappers one more time.

Primitives:

  • Have performance advantages
Wrappers:
  • Allow us to not violate the "everything is an object" principle, which means that numbers, characters, and boolean values don't violate this concept
  • Expand the possibilities for working with these values by providing convenient methods and fields
  • Are necessary when a method only works with objects
To reinforce what you learned, we suggest you watch a video lesson from our Java Course
Comments (2)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Tomas Taubinger Level 47, Brno, Czech Republic
10 January 2023
Autoboxing/Unboxing can be better understood when looking what we have on the left side of the equation: * If it is a wrapper class, we can imagine a box into which we are putting a primitive value from the right side. (Autoboxing) * Turning it around, we have a primitive variable on the left side and in order to assign anything to it, we need first to unbox the wrapper class on the right, otherwise they wouldn't match. (Unboxing) It is the variable/object on the right that's being either autoboxed or unboxed.
10 January 2023
To me it would make more sense if autoboxing/unboxing, was called autowrapping/unwrapping, since it refers to wrapper classes, not boxer classes... Go figure!