Autoboxing and unboxing in java

Autoboxing and unboxing; these terms came into the world of java from jdk 1.5. Perhaps this is one of the feature every developer uses in their every day “coding life” without knowing about it. So in this article we will discuss about this “always used still unknown” feature.

We know that there are primitives and wrapper classes(like Numbers) in java. We also know that we can convert a primitive to wrapper class and vice versa. So as a definition we can state that automatic conversion of a primitive data type into its wrapper class is called Autoboxing and converting a wrapper class into its primitive data type is called Unboxing.

And if you need to know which primitive is boxed to which wrapper class or vice versa, you can have a look into the following table.

Primitive type Wrapper class
boolean Boolean
byte Byte
char Character
float Float
int Integer
long Long
short Short
double Double

Why do we need Autoboxing and Unboxing?

  • To use them in generics. Java generic types only accepts objects. So if you want to use int or byte in a collection of generic type we need to convert them to corresponding wrapper classes.
  • To Convert primitives from/to String objects or to convert to other radix(base) like hexadecimal, octal etc.
  • To perform some functions which is available only “Object” form such as .equals(), hashcode() on a primitive data type.
  • To perform some operations such as “unary plus”, “unary minus” etc. on a wrapper object.
  • To create more readable code in less keystroke.

Autoboxing before jdk 1.5

Before jdk 1.5 if we want to use a primitive in a collection (say array list) we needed to “box” them. More precisely there were no “automatic” boxing or unboxing before jdk 1.5. An example code written in jdk 1.4 is given below.

From jdk 1.5 the “boxing” or “unboxing” happens automatically. So the following is a valid code written in jdk version 1.5.

Note:
if we want to avoid the type conversion (ie. (Integer) numberList.get(0)- in the previous example); we can use generics. So the code looks more readable as follows.

When does the Autoboxing and Unboxing happens?

As per the java documentation there are two situations which performs Autoboxing or Unboxing and they are..

  • If the method is expecting a wrapper class and the caller passes corresponding primitive type, then the primitive type will be Autoboxed. Example is given below.
  • If a primitive is assigned to a variable of corresponding wrapper class then the primitive value will be autoboxed. The code snippet below is an example for the same
  • if the above situation happens vice versa, then the Unboxing is performed by java compiler.

  • If an unary or a conditional operation is performed on a wrapper class object the Unboxing is performed by compiler. Example code is given below.

Autoboxing and Unboxing with method overloading

We have noted that primitive data value can be autoboxed to its wrapper class if the method parameter is expecting a wrapper object. But what if that method is overloaded with another method which have another type of parameter? Here are the different possibilities.

  • Widening VS Boxing

    Widening is transforming a variable to another with a wider type. For example String -> Object, byte -> int, int -> long etc. If overloading comes in a case where one method has “widened” parameter and other method has “boxed” parameter then the Widening takes priority than boxing, in other words Widening beats boxing. An example program is given below.

    The output of the above program is

  • Varargs VS Boxing

    Varargs is another feature introduced in jdk 1.5. If Varargs are used as a parameter in a method and that method is overloaded, then Boxing takes priority than Varargs, in other words Boxing beats Varargs. An example program is given below.

    The out of the above program is

At last one thing to remember; the automatic operation like “widening then boxing” is not possible. So one variable is either “widened” or “Autoboxed” at a time and both of them together is not possible. So as a result the following code will not compile.

Even though autoboxing and unboxing are very convenient feature there are some drawback which is explained in the post – 4 things to remember on autoboxing and unboxing.

Leave a Comment

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