Wrapper Class

Java has a class for every primitive data type. These classes known as Wrapper class because they wrap the primitive data types into object form. Primitive data type directly stores value in it, whereas classes use reference variable to point object. Primitive data types are better than their classes counterpart in terms of performance.

Primitive Data type Wrapper class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble


Now you might be thinking one of the quesions given below.

  • Why using primitive data types are not sufficient?.
  • Why java has provided us wrapper classes?.
  • If wrapper classes are available then why we are using primitive data types.?

We will answer each of these question one by one.



Why we use primitive data types instead of class data type

Reason : we use primitive data types instead of classes because of performance. When we use classes of primitive data types then object instantiation, object initialisation i.e. all these will affect time required to execute the program.



Using primitive data type



 int a=500;
 

Variable will get 4Byte memory. It will store value directly.


Using wrapper class


 Integer i=new Integer(500); 
 

Above instruction will allocate memory to object plus reference variable also take some memory. In order to fetch value reference variable 'i' is used. Which store reference to object.
Use of wrapper classes add extra cost of object instantiation, initialisation etc.



If primitive data types are so good then why we have wrapper class in java

Don't you think if primitive data type offer so much performance advantage then why java has Wrapper class. Let's understand answer of above question by some example.

  • Primitive data type can't be used in Collection

    Collection classes can be used only class data type, not with primitive data type. for example say we want to create a ArrayList for integers. ArrayList provide feature of array with dynamic expansion capability.

    Right Code :

    
     ArrayList<Integer> ai=new ArrayList(); // Right
      

    Wrong Code :

    
     ArrayList<int> ai=new ArrayList(); // Wrong
      

  • Null can't be used with primitive data type

    There are situation when value of variable not known at time of declaration. In this case null can be used. It can be used only with wrapper class not with primitive data type.

    Wrong Code :

    
    int i;
    i=null; // wrong
     

    Right Code :

    
    Integer i=null; // Right
     

  • Call by value v/s Call by reference

    Primitive data type arguments are passed as call by value. Sometimes there are situation where we want to pass primitive arguments as reference so that changes in method can be reflected in actual variable.




Wrapper class hierarchy


All numerics wrapper classes are subclass of Number class. Boolean and Character are the direct subclass of Object class. These wrapper class has method to convert primitive type to object and object to primitive types.