Blog Single
Wrapper class

• Wrapper class is one type of class in java which is used to convert data types into objects.
• It encloses around a primitive data type and gives it an object appearance.
• It is mainly useful in certain construct like Array List , vector which accepts only objects.

Now why do we need Wrapper class?
  • 1. To convert primitive data type to object.
  • 2. To convert string to primitive data type (called as parsing)

Before getting into some examples of wrapper class, let’s see 2 terms used here.- Autoboxing and Unboxing. It is similar to wrapping a chocolate with paper done in company and removing the paper by a kid before eating it.


  Automatic conversion of primitive type into object of their corresponding wrapper class is known as Autoboxing.


  Automatic conversion of Wrapper class object to corresponding primitive type is Unboxing.

Now let’s discuss a small program that implements all primitive data conversion to object.

public class Wrapperclass1 {
    public static void main(String[] args) {
       char c='A'; 				// character type of data
       Character chobj= c; 		// Autoboxing
       char k= chobj; 			 // Unboxing

       int i=10;				// integer type of data
       Integer iobj=new Integer(i);			// Autoboxing
       System.out.println("The object value of " +i+"is "+ iobj.intValue());	// Unboxing
       float f=1.110f;			// float type of data
					//float value should end with letter f.
                  				 //otherwise java identifies it as double value.
       Float fobj=new Float(f);			// Autoboxing
       System.out.println("The object value of " +f+"is "+fobj.floatValue());	// Unboxing
       double d=1.0;				// double type of data
       Double dobj=new Double(d);			// Autoboxing
       System.out.println("The object value of " +d+"is "+dobj.doubleValue());	// Unboxing
/*  equals() method in all wrapper class is used to check whether 
both the called object and the object passed as argument are same.
 It returns True when both objects have same value and of same type and False elsewhere. */
// double_object.equals(double_object) returns TRUE if both r double objects and contains same value. 

        double d2=(double)(fobj.floatValue());Double d2obj=new Double(d2);
        System.out.println("is double obj & float obj value same??"+dobj.equals(d2obj));

/* compareTo() method is used to compare two objects numerically. Say, v1.compareTo(v2)
// It returns 0 if both called and passing objects value are same. i.e) v1==v2
// returns negative value if called object value less than passing object value.i.e)v1v2
       System.out.println("comparing double obj & float obj value same"+dobj.compareTo(d2obj));

       byte b=10;					// byte type of data
       Byte bobj=new Byte(b);
       System.out.println("The object value of " +b+"is "+bobj.hashCode());
// returns obj.hashcode() returns hashcode of byte value-->resulting in int value

/*	converting string to int
	 done either with Integer constructor and autoboxing-- new Integer(Eno)
	using parseInt method of Integer class in java.lang package --- (Integer.parseInt(Eno))
	 using valueof method of Integer class in java.lang -----(Integer.valueOf(Eno))	*/
        System.out.println("Employee no:"+((Integer.parseInt(Eno))+1));
       System.out.println("Employee name:"+Ename+"Employee no:"+(Integer.valueOf(Eno)));
       System.out.println("Employee name:"+Ename+"Employee no:"+(new Integer(Eno)));
s    }

  • November 12,2019

    This is Nice and Useful.

Write a comment
Your email address will not be published. Required fields are marked *
Notify me of new posts by email