Java Wrapper Classes
Wrapper Classes in Java
Java Wrapper classes wrap the values of the primitive data type into objects. In other words the Java wrapper classes create objects for primitive data types. The Java wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.
For example an object of type Double, contains a field whose type is double, representing that value in such a way that a reference to it can be stored in a variable of reference type. Java Wrapper classes also provide a number of methods for converting among primitive values, as well as supporting such standard methods as equals and hashCode.
The following Java Wrapper Classes table shows which wrapper class is for which primitive data type.
Advantages of Java Wrapper Classes
- Java Wrapper Classes convert primitive data type into Object data type.
- Java Wrapper Classes are very helpful in Java Collection Framework. Because most of the Java collection classes accepts object data type.
- Java Wrapper Classes needed to use the classes of java.util package.
What is the Autoboxing in Java?
Automatic conversion of primitive data type to the object data type with corresponding Java wrapper class is called as Autoboxing.
For example convert primitive data type int to Java wrapper class Integer.
Java Autoboxing example
What is the Unboxing in Java?
Automatic conversion of Java wrapper class object to the primitive data type is called as Unboxing.
For example convert Java wrapper class Integer to primitive data type int.
Java Unboxing example