Saturday, May 20, 2017

What are wrapper classes and why do we need them?

Wrapper classes are classes for the primitive data types.

We need them because:

  1. Null can be a possible value
  2. Would get to write lot of utility methods which can make conversions easier
  3. Helps in OOP
  4. Can be used in collection as it is a class
Converting a primitive type to a wrapper class is called Autoboxing.

Converting a wrapper class object instance to a primitive type is called unboxing.

     Integer i = 12; // autoboxing

     int k = i;  //unboxing

No comments:

Post a Comment