Most of the times we use primitive data types while writing a program in any language. But there are situations where primitive data types fall short in the realm of object-oriented programming and Java is one of them. To overcome the drawbacks in Primitive datatypes, we use wrappers. This procedure is called Autoboxing. We will discuss detailed Autoboxing in Java through the docket below:
Let’s begin.
What is Boxing and Autoboxing in Java?
Boxing and Autoboxing are often used to refer to the same concept. But in reality, they are not exactly the same. Let’s first talk about the concept of boxing. What is boxing? Sounds like we are keeping something inside a box right? Yes, when we say we are boxing or wrapping a primitive data type it means we are wrapping it to form an object. Still confused? Let’s take an example.
int FirstNumber = 1;
Variable ‘FirstNumber’ is of type int which is a primitive data type. Now, what if I want to convert variable ‘FirstNumber’ into an object? Java provides a way of doing that.
Integer SecondNumber = new Integer(2);
Notice that ‘SecondNumber’ is not of type int but it is an object of type Integer. This process of converting primitive data types into an object is called boxing. You might ask how is this possible? Let’s think about what is the simplest way of accomplishing this task. We can create a class containing a single attribute of type int, a constructor which takes an int type value and assigns it to our class attribute, and some methods to manipulate this int value. To find out more about it refer this document.
We saw how int type can be converted into Java. Is there a way of converting other primitive data types into objects? Yes, Java has respective wrapper class for different primitive data types. We will look into them in the next section of this post.
Autoboxing
At this point, we know what is boxing. Now let’s understand what is Autoboxing. when the process of boxing is done by the compiler without explicitly mentioned, is called autoboxing.
Let’s understand this with an example:
import java.util.ArrayList; import java.util.List;class Box { public static void main (String[] args) { List<Integer> Mylist = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) Mylist.add(i); } }
As we discussed earlier ArrayList accepts only objects and primitive data type don’t work. In the above program, we did not convert in type to Integer type objects still the program executes without any error. How? The answer to this question is, the compiler automatically did the boxing process before adding the value to ‘Mylist’ hence the name Autoboxing.
Mylist.add(Integer.valueOf(i));
The above line of code is added in our program by the compiler.
Note- In the above line of code class name‘Integer’ is mentioned before the method valueOf( ) since valueOf( ) is a static method. For more examples refer to the docs.
Unboxing and Autounboxing
We saw how variables of the primitive data type are converted into objects. But this is only half of the story. The other half of the story is converting an object of a type wrapper class to its primitive data type is called unboxing.
For example-
Integer FirstNumber = new Integer(1); int SecondNumber = FirstNumber.intValue( ); System.out.println(SecondNumber);
Output- 1
Autounboxing- When the process of unboxing done by the compiler without explicitly mentioning is called Autounboxing.
For Example-
Integer Number = new Integer(20); int num = Number;
The above code is an example of Autounboxing. In the next section, we will learn about wrapper classes.
Wrapper Classes
We converted in type variable intl Integer type object. This integer class is a wrapper class. In Java, a wrapper class is available for each primitive data type. These wrapper classes help us in converting a variable from primitive type to respective wrapper class type object. The methods of wrapper classes are useful in manipulating the values.
The table below tells us about the primitive data type and its respective wrapper class.
Primitive Type | Wrapper Class |
boolean | Boolean |
bye | Byte |
char | Character |
float | Float |
int | Integer |
long | Long |
short | Short |
double | Double |
Notice the capitalization in the Wrapper classes.
Thus we have come to an end of this article on ‘Autoboxing in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog “autoboxing in Java” and we will get back to you as soon as possible.