WRAPPER CLASSES


There are eight wrapper classes which are equivalent to eight primitive’s datatypes. These eight wrapper classes are available in java .lang package.

Primitive datatype

byte
short
int
long                                     corresponding  wrapper
float                                              classes
double                                
char
boolean
Wrapper classes

Byte
Short
Integer           
Long
Float
Double
Character
Boolean
Advantage:-
v  You can convert primitive to objects and object to primitive
v  You can convert one datatype to another datatype.
v  In some cases we are unable to use primitive data type, we use only objects there we can get the objects which is equivalent to primitives with the help of wrapper classes.

Type of conversion: ----------------
How to convert primitive to object?
Int x=10;
ArrayList d=new ArrayList();
d.add(x); -----------------------------------No
             Byte(byte)
               Byte(String)              Every datatype have two constructor

7 datatype -  2 constructor                                     7*2=14 constructor
1 datatype    1  constructor                                character
Integer(int)
Integer(String)                                   // lo = 912345
                          Integer  i =new Integer(x);
                          Long lo = new Long(lo);
 How to convert wrapper object to primitive?
                      Integer i = new Integer(99);
                      int x=i.intValue();
                      byte b=i.byteValue();
                      short s=i.shortValue();
                      long l=i.longValue();
                      float f=i.floatValue();
                      double d=i.doubleValue();
How to convert  primitive to string object ?
v  Using toString(primitive)method
                  int x=99;
                  String s=Integer.toString(99); or x
v  By using toString() method
          byte b=10;
    Byte bb=new Byte(b);
    String s=bb.toString();
v        Using valueOf() method of String class
              double d=99.99;
        Double dd=new Double(d);
        String s=String.valueOf(dd);

How to convert  Wrapper object  to string ?
v  Using toString(primitive)method
                  int x=99;
                  String s=Integer.toString(99); or x
v  Using valueOf() method of String class
              double d=99.99;
        Double dd=new Double(d);
        String s=String.valueOf(dd);
How to convert  string  to primitive?
v  Using parseXXX() method
String s=”99”;
int x=Integer  parseInt(s);
double d=Double parseDouble(s);
v  using valueOf(str) method
string s=”999”;
int x=Integer.valueOf(s);
byte b=Byte.valueOf(s);
Integer i=Integer.valueOf(s);
Byte b=Byte.valueOf(s);
int x=i.IntValue();

How to convert  string  to wrapper object ?
v  Using valueOf(str)method
Sring s=”999”;
Integer i=Integer.valueOf(s);
v  Using constructor
Integer i=new Integer(s);
How to convert  any number type to charecter ?
No chance.
How to convert  any number type to Boolean ?
No chance .
How to convert  boolean to other number type  ?
No chance
How to convert  character to other number type  ?
Not possible  / no meaning
How to convert  string  to charecter ?
Not possible  / no meaning
How to convert  string  to boolean ?

Boolean b=new Boolean(x);
String x=”true”;
boolean b1=b.booleanValue();
boolean b2=Boolean.getBoolean(s);
How to convert  boolean to character ?
No chance / no meaning .
How to convert  character to Boolean  ?
No chance / no meaning .
How to convert   Boolean  to string  ?

boolean b=true
Boolean bb=new Boolean(b);
String s1=bb.toString();
String s2=String valueOf(b);
String s3=Boolean.toString(b);

0 comments :

Post a Comment