What is Autoboxing in Java? With Examples
Updated on Jul 03, 2023 | 7 min read | 5.9k views
Share:
For working professionals
For fresh graduates
More
Updated on Jul 03, 2023 | 7 min read | 5.9k views
Share:
Table of Contents
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.
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.
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:
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:
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.
Let’s begin with an autoboxing in Java example:
int ‘i’ = 10;
Integer ‘j’ = i; // autoboxing
In the above example, we’re assigning the value of the primitive integer variable’ i’ to its corresponding wrapper class object ‘j’ using autoboxing.
Consider the following example of unboxing in Java:
Integer ‘i’ = 10;
int ‘j’ = ‘i’; // unboxing
In this example, we’re assigning the value of the wrapper class object ‘i’ to the primitive integer variable’ j’ using unboxing.
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.
MyList.add(Integer.valueOf(i));
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.
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.
While autoboxing and unboxing are convenient features in Java, they can impact performance, especially when used in large collections such as arrays or lists. This is because autoboxing and unboxing contain developing and manipulating wrapper objects, which may be inefficient compared to running with primitive types without delay. As a general rule, avoiding autoboxing and unboxing in performance-vital sections of code is recommended and, as a substitute, using primitive sorts on every viable occasion.
There are positive regulations to remember when working with autoboxing in Java. For instance, now, not all primitive types may be autoboxed. Only the primitive types may be autoboxed: ‘boolean’, ‘byte’, ‘char’, ‘quick’, ‘int’, ‘long’, ‘flow’, and ‘double’.
Additionally, whilst autoboxing a value, Java will most effectively create a brand new object if the fee is outdoor and the wrapper class can represent the variety of values. For instance, autoboxing the cost ’10’ to an ‘Integer’ item will not create a new object because ’10’ is in the range of values that can be represented by means of ‘Integer’.
While autoboxing can be handy, it can also lead to surprising consequences in some instances.
For instance, recall the code given below:
Integer ‘i’ = 10;
Integer ‘j’ = 10;
System.out.println’(i == j)’;
In this example, we’d assume the output to be actual because each ‘i’ and ‘j’ have the identical value of ’10’. However, the output will be fake because’ i’ and ‘j’ are unique items, and the ‘= =’ operator compares object references, not values. To avoid this difficulty, we can use the ‘equals()’ approach to compare the values of ‘i’ and ‘j’.
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 |
Java has various supplementary autoboxing and unboxing features, such as the ability to convert between primitive types and their related wrapper classes utilising wrapper classes and methods. For example, the ‘Integer.parseInt()’ function has been used to convert an integer string representation to its corresponding primitive integer type; an ‘Integer.valueOf()’ method can be used to turn a primitive integer into its corresponding wrapper object. Besides that, Java includes wrapper classes comprising ‘Double,’ ‘Boolean,’ and ‘Character’ for additional base types that may be used similarly to Integer.
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.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources