Last Updated: 28 May, 2022
Java wrapper class provides a mechanism to convert primitive data types (int, float, char, boolean, etc.) into corresponding objects (Integer, Float, Character, Boolean, etc.). A Wrapper class wraps or contains primitive values into a wrapper class object.
java.lang
Package provides all 8 Wrapper Classes for each of the primitive data.In the table, we can see the Primitive Data types and their Corresponding Wrapper class:
Primitive Type | Wrapper Class | Constructor Arguments |
---|---|---|
boolean | Boolean | boolean or String |
char | Character | char |
byte | Byte | byte or String |
int | Integer | int or String |
float | Float | float, double or String |
double | Double | double or String |
long | Long | long or String |
short | Short | short or String |
java.util
package only deals with objects.Autoboxing and Unboxing in Java
Autoboxing: Autoboxing is the process of converting a primitive datatype to its corresponding Wrapper class.
For example, int to Integer, char to Character, double to Double, float to Float, long to Long, boolean to Boolean, short to Short, byte to Byte.
Example 1 : Primitive to Wrapper Conversion (Autoboxing)
Output
Wrapper value : 100
Wrapper name: class java.lang.Integer
ArrayList value: 100
Unboxing: It is a reverse process of autoboxing, It converts a Wrapper class object into its corresponding Primitive Datatype.
For example, Integer to int, Character to char, Double to double, Float to float, Long to long, Boolean to boolean, Short to short, Byte to byte.
Example 2 : Wrapper to Primitive Conversion (Unboxing)
Output
Primitive value 1 : 100
Primitive value 2 : 500
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com