What is Autoboxing in Java? With Examples

In the contemporary world, Java is the renowned choice for scripting Android applications. It also finds extensive demand in the machine learning environment, cloud applications and IOT technology. The developers need to have a thorough understanding of the recent trends in Java programming. A lucrative professional prospect is assured for Java developers because the language has a firm presence in the enterprise. Java is not only a leading object-oriented language in the android mobile application market but also in the field of backend market. 

Basics of autoboxing in Java:

While scripting a program, primitive data types are used in most of the programming languages. However, in object-oriented programming languages such as Java, the primitive data types are not effective in some scenarios and they fall short in the realm. To overcome the limitations of the primitive data types, wrappers are used. This phenomenon is called autoboxing. Let us discuss more about autoboxing in Java in the subsequent sections. 

Check out our free technology courses to get an edge over the competition.

Explore our Popular Software Engineering Courses

Autoboxing and Autounboxing:

Autoboxing is the process of conversion of a primitive value into a wrapper class’s object. For example, an int data type is converted to an integer class. The autoboxing function is performed by a Java compiler if the primitive value is:

  • Assigned to a variable of respective wrapper class.
  • Passed as an argument to a method that uses an object of the respective wrapper class.

Autoboxing is a reverse of the autoboxing function. The conversion of a wrapper type object to its respective primitive value is referred to as unboxing. Let us consider an example of an Integer object converted into an int data type. The Java compiler performs unboxing when a wrapper class object is:

  • Assigned a variable of the respective primitive type.
  • Passed as an argument to a method that requires the corresponding primitive type’s value.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

In-Demand Software Development Skills

Autoboxing in Detail:

The terms boxing and autoboxing are alternately used in many cases to describe the same concept. However, there is a subtle difference between the two terms. The term ‘boxing’ refers to wrapping of a primitive data type to form an object. Let us consider an example to understand this concept. Consider the Java code statement written below. 

int FirstNumber = 1;

In this statement, the variable ‘FirstNumber’ is of int data type which is primitive. It can be converted to an object using the statement below. 

Integer SecondNumber = new Integer(2);

The variable SecondNumber in the above statement is not of the primitive int data type. Instead, it is an Integer object. This process of conversion of primitive types of data into an object is referred to as boxing. The easiest way of performing this task is by developing a class that contains a single attribute of int data type. A constructor of int type value is assigned to the class attribute. Several methods are used to manipulate the int value. The wrapper classes in Java can be used to enable the conversion of other data types into an object. 

Autoboxing is the function performed automatically by the compiler without mentioning it explicitly.   


This is the line added by the compiler into our program.

In the above code statement, the name of the class ‘Integer’ is specified before mentioning the method valueOf() because valueOf() is a static method. 

Read our Popular Articles related to Software Development

What is unboxing in Java?

Now that we have understood the process of conversion of primitive data types into objects, let us look into the process that does the reverse function of boxing. Unboxing, as mentioned earlier, refers to the process of conversion of an object of wrapper class type into its primitive data types.

When the process of unboxing is performed automatically by the compiler without mentioning it explicitly in the program, it is referred to as Auto unboxing. Let us consider the example shown below to understand the auto unboxing. 

More about Wrapper Classes:

In the example for autoboxing, the variable I is of int data type and the object Integer is a wrapper class. The wrapper class in Java is available for all the primitive data types. These classes enable the user in the conversion of a variable from primitive data type to the corresponding object of wrapper class type. The wrapper class methods are used in the manipulation of the values. The primitive data types and their respective wrapper classes are summarized in the table below. Note that the name of the wrapper classes begin with an uppercase letter. 

Primitive data type

Corresponding wrapper class

byte Byte
boolean Boolean
char Character
int  Integer
float Float
Short Short
Long Long
double Double

Merits of Autoboxing and auto unboxing in Java:

  • The process of autoboxing and unboxing enable the programmers to script a well structured and easily readable code.
  • The process also enables the users to switch between the primitive data types and the objects of wrapper class alternately. Hence, the explicit typecasting is not required.

If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s Executive PG Programme in Software Development – Specialisation in Full Stack Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

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

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

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

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks