In this tutorial we will learn about the concept of Autoboxing and Unboxing in core java. But before that let's understand what is type Wrappers. Java uses primitive data types such as int, double, float etc. Despite the performance benefits offered by the primitive data types, there are situations when you will need an object representation of the primitive data type. For example, many data structures in Java operate on objects. So you cannot use primitive data types with those data structures.

Author:Taulabar Douzuru
Language:English (Spanish)
Published (Last):7 January 2005
PDF File Size:7.41 Mb
ePub File Size:2.7 Mb
Price:Free* [*Free Regsitration Required]

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:. Boxing and Autoboxing are often used to refer to the same concept. But in reality, they are not exactly the same.

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? Java provides a way of doing that.

This process of converting primitive data types into an object is called boxing. You might ask how is this possible? 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. At this point, we know what is boxing. In the above program, we did not convert in type to Integer type objects still the program executes without any error.

For more examples refer to the docs. 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. Autounboxing- When the process of unboxing done by the compiler without explicitly mentioning is called Autounboxing. The above code is an example of Autounboxing. In the next section, we will learn about 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. Got a question for us? Already have an account? Sign in. What Is Java? What are the different Applications of Java? Environment Setup. How To Set Path in Java? How to Compile and Run your first Java Program?

Control Statements. What is for loop in java and how to implement it? What is a While Loop in Java and how to use it? What is for-each loop in Java? What is a Do while loop in Java and how to use it? What is a Switch Case In Java? Java Core Concepts. What are the components of Java Architecture? What are Comments in Java? What is a Constructor in Java? What is the use of Destructor in Java? What Are Methods In Java? What is a Constant in Java and how to declare it? What is JIT in Java? What is the role for a ClassLoader in Java?

What is an Interpreter in Java? What is Bytecode in Java and how it works? What is a Scanner Class in Java? What is the Default Value of Char in Java? What is a Static Keyword in Java? What is Ternary Operator in Java and how can you use it? What is Modulus in Java and how does it work? What is the difference between Method Overloading And Overriding? How to Create a File in Java? What is a Comparator Interface in Java?

What is a Java Thread Pool and why is it used? Synchronization in Java: What, How and Why? What is Wait and Notify in Java? What is Power function in Java? How to Convert Int to String in Java? What is PrintWriter in Java and how does it work? What is Trim method in Java and How to Implement it?

How do you exit a function in Java? What is AutoBoxing and unboxing in Java? What is Factory Method in Java and how to use it? What is Dictionary in Java and How to Create it? What is Stack Class in Java and how to use it? Java Strings. What is the concept of String Pool in java? What is the difference between Mutable and Immutable In Java? Objects and Classes. What are the different types of Classes in Java? What is a Robot Class in Java? What is Integer class in java and how it works?

What is System Class in Java and how to implement it? Char in Java: What is Character class in Java? What is the Boolean Class in Java and how to use it? Java OOPS. How to Implement Nested Class in Java? What is the Use of Abstract Method in Java? What is Association in Java and why do you need it?

What is the difference between Abstract Class and Interface in Java? What is Runnable Interface in Java and how to implement it? What is Cloning in Java and its Types? What is Semaphore in Java and its use? Java Collections.


What is AutoBoxing and unboxing in Java?

Since JDK version 5, Java provides two features, called autoboxing and auto-unboxing. They typically mean automatic conversion between primitive type values to its corresponding wrapper classes. This idea simplified coding of several algorithms, removing the trouble of explicit boxing and unboxing of values. The conversion is implicit.


Autoboxing and Unboxing:

The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn't need to write the conversion code. The automatic conversion of wrapper class type into corresponding primitive type, is known as Unboxing. Let's see the example of unboxing:.

Related Articles