Wrapper Classes in Java



Wrapper Class:

In wrapper class we have Objects which stores primitive data type or which wraps primitive data types.

Advantages:


  1. We cannot pass Primitive data type data between two application over the Internet.So Wrapper class needed in that case.
  2. When we send data to server then the server expects data to be in object format rather than primitive type of data.
  3. Objects in wrapper class can hold primitive data types.

    Syntax:

                    Integer  i = new Integer(123);
                    Float f = new Float(45f);

same for the  remaining wrapper classes.


Primitive Data Types
Wrapper Classes

int
Integer
byte
Byte
char
Character
short
Short
long
Long
float
Float
double
Double
boolean
Boolean


Wrapper  classes have boxing and unboxing.


Boxing:

          boxing means converting primitive data type to objects is boxing. 

 Ex:
                    int i=10;   // primitive type
           

               Integer obj=new Integer(i);   //coverting to objects
          

                System.out.println(obj);


Output:

           10


UnBoxing:

              unboxing means converting objects to corresponding primitive data type is unboxing.

Ex:


               int i=10;   // primitive type
           

               Integer obj=new Integer(i);   //coverting to objects

                int  j = obj.intValue();  //converting to primitive type:unboxing
   

                System.out.println(j);

Output:

          10















No comments:

Post a Comment