A brief introduction about Autoboxing and autounboxing

Posted By : Nanda Ram | 24-May-2018
Until 1.4 version we can't provide primitive value in the place of wrapper objects & wrapper objects in the place of primitive. All required conversion should be performed explicitly by the programmer.
Example:
                 ArrayList l = new ArrayList();
		         l.add(10);   // C:E: compile time exception 
		         Integer i = new Integer(10);
		         l.add(i);     // no complie time exception 
		         Boolean B = new Boolean(true);
                 if(B)
                {
		            System.out.println("Hello"); 
                 }		                  // C:E :- Incompatible types 
		                                    found : Boolean
						                     required : boolean
		            boolean b = B.booleanValue();
		            if(b)                            // will work fine
		           {
                       System.out.println("Hello");
		            }
 
But from 1.5 version onwards in the place of wrapper objects we can provide primitive value & in the place of primitive value, we can provide wrapper objects. All the required conversions will be performed automatically by the compiler, these conversions are called autoboxing and auto-unboxing.
Autoboxing 
           Automatic conversion of primitive value to the wrapper objects by the compiler is called autoboxing.
     Example:
                Integer i =10;   
               Above statement, the compiler converts int to Integer automatically by autoboxing.
 Auto-unboxing
                Automatic conversion of wrapper objects to the primitive type by the compiler is called Auto - unboxing
    Example: 
               int i = new Integer(10);
                Above statement, the compiler converts Integer to int automatically by auto-unboxing. 
   Example  1 :
           Integer i = 10;
          After compilation this line becomes 
          Integer  i = Integer.valueOf(10);
         i.e, Autoboxing concept internally implemented by using valueOf() 
   Example  2: 
             Integer I = new Integer(10);
              int i = I;
             After compilation this line will becomes
             int i = I.intValue();
            i.e, Auto- unboxing concept internally implemented by using xxxvalue()
Note: 
     Because of autoboxing & auto-unboxing. from 1.5 version onwards there is no difference between primitive value & wrapper objects. we can use interchangeably.
Example:
 

        class Test 
        {
          Static Integer I = 10;    // Auto boxing
          public static void main(String args[])
          {
		  int i= I;       // auto - unboxing
		   display(i);
		   }
		   public static void main(Integer m)
           {
             int j = m;
             System.out.println(j);	     // 10		 
		 }
	 }
 
Conclusion:
             By Autoboxing if an object is required to create compiler won't create that objects immediately first check is an object already created.
             If it is already created then it will reuse existing object instead of creating a new one.
             If it is not there, then only a new object will be created   
             But this rule is applicable only in the following cases
             1. Byte  ----> Always
              2. Short ----> -128 to 127
              3. Integer ---> -128 to 127
              4. Long   ----> -128 to 127
              5. Character ---> 0 to 127
              6. boolean ----> Always
             Except for the above range in all other cases compulsory a new object will be created.

About Author

Author Image
Nanda Ram

Nanda Ram is a Java Developer, he keep conscientious about his task to complete it in a effective and efficient manner .

Request for Proposal

Name is required

Comment is required

Sending message..