How to Use Autoboxing and Unboxing in Java?
Autoboxing and Unboxing help simplify code by converting the primitive type classes into their relative wrapper classes automatically. By doing this, the size and chances of generating errors can be reduced. These concepts also play a vital role in improving the type of safety by checking the appropriate conversion of the values.
This article demonstrates the procedure for utilizing Autoboxing and Unboxing in Java.
How to Use Autoboxing and Unboxing in Java?
Autoboxing and Unboxing seamlessly integrate with work APIs and support the use of primitive types in collection classes that require objects. Moreover, they facilitate the interoperability of legacy code that uses primitive types with newer code that requires wrapper objects.
Follow the below examples for a better understanding:
Example 1: Using Autoboxing in Java
In Autoboxing, the conversion from a primitive value to a relative/corresponding “wrapper” class happens automatically. This reduces the chance of making errors, time, and effort.
Visit the below code for a better understanding:
import java.util.Map;
public class AutoboxingOnMap{
public static void main(String[] args) //Creation of main() method
{
Map mapping = new HashMap();
int key = 1;
int value = 35;
mapping.put(Integer.toString(key), value); // Autoboxing: int to Integer
System.out.println("Using Autoboxing: " + mapping);
}
}
In the above code:
- First, the utilities are imported for accessing the feature of Map.
- Next, declare a “Map” with the name of “mapping” in the “main()” method.
- Then, declare two “int” type variables named “key” and “value” and initialize them with “1” and “35” values.
- Then, use the “Integer” wrapper class to select the “int” type variable by “autoboxing”. And convert the “key” into the string and insert both “key” and “value” in the map by utilizing the “put()” method.
- In the end, display the results on the console.
After the end of the compilation phase, the output shows below:
Example 2: Using Unboxing in Java
Unboxing is a concept or process that works opposite to “Autoboxing”. To be clear, it converts the wrapper class values into the primitive types. The intValue(), and doubleValue(), are utilized in the process of unboxing.
Let us visit the below code for a better explanation:
import java.util.Map;
public class UnboxingOnMap{
public static void main(String[] args) //creation of main() method
{
Map<String, Integer> mapping = new HashMap<>();
mapping.put("1", 35);
int retrievedValue = mapping.get("1"); // Unboxing: Integer to int
System.out.println("Retrieved Value Using Unboxing: " + retrievedValue);
}
}
In the above code:
- First, the utilities for the “Map” and “HashMap” are imported.
- Next, the “Map” is created that stores data in “String” and “Integer” formats. Also, insert a single element in the map by utilizing the “put()” method.
- Then, create an “int” primitive type variable that gets the element from the map having the key of “1”.
- In the end, display the results on the console.
After the end of the compilation phase:
The snapshot shows the element has been retrieved and the concept of Unboxing is also utilized.
Conclusion
Autoboxing converts selected primitive class data into wrapper class automatically and in the case of Unboxing the wrapper class gets converted into the primitive class. Autoboxing and Unboxing provide convenience, type safety, and interoperability. They are valuable in various real-time scenarios including collections, database operations, GUI programming, and mathematical calculations. This article demonstrates the procedure to utilize Autoboxing and Unboxing in Java.
Source: linuxhint.com