Friday, 16 January 2015

Wrapper Classes in java

Wrapper classes are useful to convert primitives to and from String objects
To wrap primitive values in an object so that the primitives are used as being
1. added to collections
2. return from a method with an object value

These are used to "wrap" the primitive data type into an object of that class



Method
Purpose
parseInt(s)
returns a signed decimal integer value equivalent to string s
toString(i)
returns a new String object representing the integer i
byteValue()
returns the value of this Integer as a byte
doubleValue()
returns the value of this Integer as an double
floatValue()
returns the value of this Integer as a float
intValue()
returns the value of this Integer as an int
shortValue()
returns the value of this Integer as a short
longValue()
returns the value of this Integer as a long
int compareTo(int i)
Compares the numerical value of the invoking object with that of i.
o- if equal ; -ve if low value ; +ve if high value






    public class WrapperDemo {  
       
       public static void main (String args[]){  
           Integer i1 = new Integer (25);  
           Integer i2 = new Integer ("25");  
           Integer i3= new Integer (35);  
           //compareTo demo  
       System.out.println("Comparing using compareTo Obj1 and Obj2: " + i1.compareTo(i2));  //0
      System.out.println("Comparing using compareTo Obj1 and Obj3: " + i1.compareTo(i3));  // -1
           
           //Equals demo  
      System.out.println("Comparing using equals Obj1 and Obj2: " + i1.equals(i2));  //true
      System.out.println("Comparing using equals Obj1 and Obj3: " + i1.equals(i3));  //false
                   
           Float f1 = new Float("2.5f");  
           Float f2 = new Float("2.43f");  
           Float f3 = new Float(2.5f);  
     System.out.println("Comparing using compare f1 and f2: " +Float.compare(f1,f2));  //-1
      System.out.println("Comparing using compare f1 and f3: " +Float.compare(f1,f3)); //0
           
           //Addition of Integer with Float  
           Float f = i1.floatValue() + f1;  
           System.out.println("Addition of i1 and f1: "+ i1 +"+" +f1+"=" +f );  //25+2.5

How to create wrapper objects?

1. wrapper constructors
2. valueof() methods
3.  using wrapper conversion utilities
  1.   xxxvalue()
  2. parsexxx() and valueof()
  3. toString()
  4. to xxxString()


1. wrapper constructors:
Except character all other wrapper classes  provide 2 constructors

Ex:
1.
Integer i1=new Integer(42);
Integer i1=new Integer(“42”);

2.
Float f1=new Float(3.14f);
Float f1=new Float(“3.14f”);

3.
Character ch=new Character(‘c’);

4.
Boolean- wrapper classes take either true/false or string
Ex:
    Boolean b=new Boolean(“false”);

valueof() methods:

It has 2 arguements where 1st arguement is String representation of appropiate type of primitive
2nd method takes additional arguement
Ex:

Integer i=Integeer.valueof(“101011”,2)
It equals to 43 which is asigned to ‘i’.



3. using wrapper conversion utilities
1. xxxvalue()
it is used to convert the values of wrapped numeric to primitive. All the methods have no-arg methods

Ex: Integer i= new Integer(42);
byte b=i.byteValue();
short s=i2.byteValue();


2. parsexxx and valueof()

It takes string as an argument and throws NumberFormatException if string is not properly formed.

Difference between parsexxx and valueof()


parsexxx
valueof()
it returns named primitive
it returns a newly created wrapped object of the type that invoked the method


Ex:
double d=Double.parseDouble(“3.14”);
System.out.println(“d4=”+d4);

output: d4=3.14

Long l2=Long.parseLong(“101010”,2);
System.out.println(“l2=”+l2);

output: 42

toString()
allows you to get a meaningful representation of a given object


1. All wrapper classes has no-arg, non static, instance version of toString()
    Double d=new Double(“3.14”);
    System.out.println(“d=”+d.toString());
2. all numeric wrapper classes provide an overloaded static toString()
    String d=Double.toString(3.14);

toXxxString()
take int/long and return String representation of converted number
    String s=Integer.toHexString(254);
    System.out.println(“254 is “+s);
output:
254 is fe

No comments:

Post a Comment