Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What is Autoboxing in Java? With Examples

Updated on 03 July, 2023

5.82K+ views
7 min read

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.

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.

Autoboxing and Unboxing in Java: Examples

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.

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.   

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. 

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. 

Performance Impact of Autoboxing in Java

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.

Rules for Autoboxing in Java

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’.

Examples of When Autoboxing Can Lead to Unexpected Results

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’.

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 enables 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 the wrapper class alternately. Hence, the explicit typecasting is not required.

Additional Features Related to Autoboxing and Unboxing

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.

Frequently Asked Questions (FAQs)

1. What makes autoboxing different from unboxing in Java?

Autoboxing is converting a primitive type to its corresponding wrapper class. At the same time, unboxing is the other system of converting a wrapper elegance object to its corresponding primitive type.

2. When shall I keep away from the use of autoboxing in Java?

Autoboxing can affect overall performance, particularly in big collections with arrays or lists. As a preferred rule, avoiding autoboxing and unboxing in overall performance-vital sections of code is recommended, and alternatively, using primitive types on every occasion is viable.

RELATED PROGRAMS