Wrapper Classes
Wrapper classes are primarily used to convert a primitive type to Object and Object types into primitive. They wrap the primitive values into Objects and vice-versa.
When a primitive type is converted to object, then it is called boxing. Similarly, when an object type is converted into primitive then it is called unboxing.
Sometimes java compiler automatically converts the primitive values into objects implicitly, then it is called as Auto-boxing. Similarly, when the java compiler converts objects into primitive values then it is called Auto-unboxing.
Since Java is an object-oriented programming language sometimes working with data structures we cannot use primitive values.
Some classes like ArrayList and Vector accepts only objects, not primitives. In those cases, we need to convert primitive types into an object.
The list of Wrapper Classes is Character, Byte, Short, Long, Integer, Float, Double and Boolean.
Example for Wrapper Classes:
public class WrapperExample {
public static void main(String[] args) {
int a = 50;
// boxing
Integer a1 = Integer.valueOf(a);
// Autoboxing
Integer a2 = a;
System.out.println(a + " " + a1 + " " + a2);
}
}
0 Comments