A Wrapper class is a class which contains the primitive data types (int, char, short, byte, etc). In other words, wrapper classes provide a way to use primitive data types (int, char, short, byte, etc) as objects. These wrapper classes come under java.util package.
Autoboxing is used to convert primitive data types into corresponding objects.
public class AutoBoxingTest { public static void main(String args[]) { int num = 10; // int primitive Integer obj = Integer.valueOf(num); // creating a wrapper class object System.out.println(num + " " + obj); } }
10 10
Unboxing is used to convert the Wrapper class object into corresponding primitive data types.
public class UnboxingTest { public static void main(String args[]) { Integer obj = new Integer(10); // Creating Wrapper class object int num = obj.intValue(); // Converting the wrapper object to primitive datatype System.out.println(num + " " + obj); } }
10 10