Pages

Wednesday, December 18, 2013

Java - Wrapping class

It is the kind of object

Upto Java 1.4---->boxing method has to be created
After the java 1.5----> Auto boxing and Auto boxing is created.

          Primitive Datatype                      Wrapper
-----------------------------------------------------------------
          byte                                              Byte
          int                                                 Int
          short                                             Short
          char                                              Char
          boolean                                         Boolean
          long                                               Long
          float                                               Float
          double                                           Double

This wrapping classes are predefined classes in the java library. It can be used anywhere in the program and packages.

tostring()
valueof()

The above method can also be used in the wrapping class.

This class also can created the instance for particular class which can be used to handle the method.

Integer n = new Integer(35.5);
Integer n = new Integer("34.5");
int x=35.5;
Integer n = new Integer(x);

     The above all way may help to pass the integer value to the method n.intValue();

Some of the example for wrapping class which can be follow  upto java 1.4,

Integer x = new Integer(50);
int y = x.intValue();
System.out.println(y);  //// unboxing----->convert wrapping class into primitive datatype

++y;
System.out.println(y)
x=newInteger(y);      ////boxing------->convert primitive datatype into wrapping class
System.out.println(x);

output:
50
51
51

Some of the example for wrapping class in the java 1.5 and after that

Integer x = new Integer(50);  ///Autoboxing and Autounboxing
x++;
System.out.println(x);

output:
50

Example:

Integer x = new Integer(100);
Integer y = new Integer(100);

x!=y;   -------->TRUE  (It check the two objects)

x.equalsof(y);  ----->TRUE  (It check the values)

x==y;  ---->TRUE  (It check the primitive datatype)
 




No comments:

Post a Comment