Monday 22 July 2013

What are Wrapper classes in java?





Java uses primitive types ( int, char, double etc.) to hold the basic data types supported by the language. Sometimes it is required to create an object representation of these primitive types as there are collection classes that deal only with objects. One needs to wrap the primitive type into a class.
Example: int a=1;    //Primitive data type int 

                 Integer  I  = new Integer(1);    //Wrapper class Integer for int primitive data type

To satisfy this need, java provides classes that correspond to each of the primitive types as:
All byte, Short, long, double use Byte, Short, Long, Double class as wrapper respectively similarly char has Character and boolean has Boolean wrapper class. Thus, they are commonly referred to as type wrapper.
So we can define type wrapper is classes that encapsulate a primitive type within an object.

No comments:

Post a Comment