Pageviews

Thursday 20 June 2013

Wrapper Classes in Java

by Unknown  |  in Wrapper class at  Thursday, June 20, 2013

Wrapper Classes:The primitive data types in Java such as Int,Char,Float etc has a class dedicated to it.Sometimes it is required to create an Object for these data types.The classes which provided this functionality in Java are known as 'Wrapper classes' in Java,because basically they 'wrap' the data type of the class to an Object.The wrapper classes are present in Java.lang package.

Advantages of Wrapper Classes in Java:


  • They provide mechanism to wrap primitive data types,so that primitives can do the activities reserved for the objects like being added to Array lists,Hash sets,Hash Maps etc.
  • They provide utilities functions for primitives like converting them to and from String objects,converting them to various bases like Binary,Hexadecimal and comparing various objects.
  • They are used basically for Type Casting.


Following statements illustrate the use of Wrapper classes:

int x=10;
Integer y=new Integer(x);

The first statement declares an int variable x,initialized with value 10,whereas the second statement creates an object for Integer y.The object initializes and reference to the object is assigned to the variable y.

In the above two statements,we had wrapped the data type Integer and now we are going to unwrap it,it is illustrated by following statements:

int k=y.intValue();
System.out.println(k*k); // Result is100



In the above codes,we had used Wrapping and Un-Wrapping of data types in Java.Each data type has a wrapper class associated to it,the picture shows the list

Wrappler class



The wrapper classes perform wrapping and un-wrapping of data types,here is the program of implementing wrapper classes in Java.

WAP to implement Wrapper classes in Java

class wrappler
{
public static void main(String args[])
{
byte grade=2;                                               //Byte data type
int marks=50;                                               //Integer data type
float pric=5.6f;                                             //Float data type
double rate=50.6;                                        //Double data type

Byte g1=new Byte(grade);                          //Object created for Byte data type
Integer m1=new Integer(marks);                // Object created for Integer data type
Float f1=new Float(pric);                            //Object created for Float data type
Double r1=new Double(rate);                      //Object created for Double data type

System.out.println("Values are");
System.out.println("Byte object g1:"+g1);
System.out.println("Integer object m1:"+m1);
System.out.println("Byte object f1:"+f1);
System.out.println("Double object r1:"+r1);

}
}

The output for the program is:

Wrapper class




Wrapper classes are not basically used in programs these days,because Type Casting is also supported by Java.But still,we can use Wrapper class according to our requirement.















0 comments:

Proudly Powered by Blogger.