Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 24 April 2017

Wrapper Class in Java

 

Wrapper Class

Wrapper class in java is used to convert primitive types into object and object into primitive types. All the java wrapper classes are immutable and final.

Since J2SE 5.0, Autoboxing and Unboxing allows easy conversion automatically between primitive types and their corresponding wrapper classes.

Autoboxing : The automatic conversion of primitive types into object is known as autoboxing in java.

for example - conversion of int to Integer, double to Double, long to Long etc.

Unboxing : The automatic conversion of object into primitive types is known as unboxing in java. Unboxing is the reverse process of autoboxing. 

for example - conversion of Integer to int, Double to double, Long to long etc.

There are 8 wrapper classes in java.lang package. The list of eight wrapper classes is given below.


Wrapper Class in Java

Creating Object of Wrapper Class

Suppose we create an object of Integer wrapper class. There are two ways we can

Integer i = new Integer("32");
Integer i = new Integer(32);

By passing String or variable of the same data type as that of the type to which the wrapper class corresponds except for the character wrapper class.

 Wrapper Class Example : Converting Primitive to Wrapper

In this examples we will converting int(primitive) to Integer(wrapper).

For example 1 :

class WrapperClassFirst
{
public static void main(String args[])
{
int a = 30;
Integer i = Integer.valueOf(a);//converting int to Integer
Integer k = a;//compiler will write internally Integer.valueOf(a)
System.out.println(i);
System.out.println(k);
}


output : 30
              30

For example 2 :

class WrapperClassSecond
{
void age(Integer i)
{
System.out.println(i);
}
public static void main(String args[])
{
WrapperClassSecond  a = new WrapperClassSecond();
//passed int (primitive type), it will converted into Integer object
a.age(18);
}
}

output : 18

In the above example, we define a method which excepting Integer wrapper class object but we passed value as parameter of primitive type when we call a method. It will converted primitive to Integer at run time.

For example 3 :

Autoboxing in case of Collection Framework.

ArrayList<Integer> al = new ArrayList<Integer>();
al.add(100);//Autoboxing int primitive to Integer wrapper
al.add(101);//Autoboxing int primitive to Integer wrapper


Wrapper Class Example : Converting Wrapper to Primitive

In this example we will convert wrapper to primitive types.

For example 1 :

class WrapperClassFirst
{
public static void main(String args[])
{
Integer i = new Integer(5);
int a = i.intValue();//converting wrapper to primitive
int b = i;//compiler will add i.intValue()
System.out.println(a+" "+b);
}
}

output : 5 5

For example 2 :

class WrapperClassSecond
{
public static void age(int i)
{
System.out.println(i);
}
public static void main(String args[])
{
Integer a = new Integer(60);
//passed Integer wrapper class object ,will converted into int primitive
age(a);
}

}

output : 60

In the above example, there is static method age with primitive type parameter but we passed Integer wrapper class object when we call method.

For example 3 :

In case of collection Framework class.

ArrayList al = new ArrayList();
int i = al.get(0);//Unboxing, get method returns Integer object


Some Points about Wrapper Class

  • Wrapper class can be used to achieve polymorphism.
  • Primitive types cannot by null but wrapper class can be null.
  • 'Number' class is the super class of all wrapper classes in java. Which is available in java.lang package



Share:

3 comments:

  1. Nice post about java wrapper class. thank you

    ReplyDelete
  2. Nice site....Please refer this site also Our vision success!Training are focused on perfect improvement of technical skills for Freshers and working professional. Our Training classes are sure to help the trainee with COMPLETE PRACTICAL TRAINING and Real time methodologies
    Asp.Net Training in Chennai | Dot Net Training Center in Chennai | Dot Net Coaching Centers in Chennai
    Software Testing Institutes in Chennai | Manual Testing Training in Chennai
    Java Training Institute in Chennai | Core Java Training in Chennai | Java Course and Certification
    PHP Training Institute in Chennai | Best PHP Course in Chennai | Best PHP Training Institutes

    ReplyDelete

  3. Thanks, this is generally helpful.
    Still, I followed step-by-step your method in this Java online training
    Java online course

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate