Tuesday, 30 April 2013

Wrapper Classes

         It provides flexible functionality of wrapping primitive values in to an object so that the primitives can be included in activities reserved for objects.

Constraints Imposed Here
  
        We cannot derive a subclass from wrapper classes because it is declared as final. So, the constant functionality of this class never be changed or modified.

Super Classes


byte, short, int, float and double - number class[abstract class]

Boolean and Character – Object

Package


       The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.

Sample



int x = 20; - It declares an int variable named x and initializes it with the value 25

Integer y = new Integer(33); - Instantiates an Integer object. The object is initialized with the value 33 and a reference to the object is assigned to the object variable y.

Sample Program


public class WrapperMain {

      public static void main(String[] args) {

            byte regno = 38;

          int totalMark = 416;

          float examFees = 3000.50f;                          

          double annualFees = 80000.50d;

       
          // data types to objects - wrapping          

          Byte regnoObj = new Byte(regno);                 

          Integer totalMarkObj = new Integer(totalMark);

          Float examFeesObj = new Float(examFees);

          Double annualFeesObj = new Double(annualFees);

                                                            

          System.out.println("Values of Wrapper objects (printing as objects)");

          System.out.println("Byte object:  " + regnoObj);

          System.out.println("Integer object:  " + totalMarkObj);

          System.out.println("Float object:  " + examFeesObj);

          System.out.println("Double object:  " + annualFeesObj);

       
          // objects to data types - unwrapping

          byte byteValue = regnoObj.byteValue();               

          int integerValue = totalMarkObj.intValue();

          float floatValue = examFeesObj.floatValue();

          double doubleValue = annualFeesObj.doubleValue();

                                                                           
          System.out.println("Unwrapped values (printing as data types)");

          System.out.println("byte value: " + byteValue);

          System.out.println("int value: " + integerValue);

          System.out.println("float value: " + floatValue);

          System.out.println("double value: " + doubleValue);

      }

}

Mostly Used Methods



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

longValue() - returns the value of this Integer as a long

shortValue() - returns the value of this Integer as a short

toString() - returns a String object representing the value of this Integer
Usage

          It provides various utility functions for primitive types. Like primitive to String, String to binary or hexadecimal etc.

Most of the collection classes store objects and not primitive data types.