Google+

251. AutoBoxing








AutoBoxing is the process by which a primitive type is automatically boxed into its equivalent type wrapper whenever an object of that type is needed. There is no need to explicitly construct an object. In order to understand the AutoBoxing clearly, we have to first understand the manual Boxing process -

Example for Manual Boxing -

Integer obj =  new Integer(100);  //We have created an object of Integer Wrapper Class here

Example for AutoBoxing -

Integer obj = 100;  //Here we have not created any object of Integer Wrapper Class, its done automatically by Java.

While implementing AutoBoxing in the above example, we have not used new operator for creating an object for Integer Wrapper Class. Instead we have directly assigned integer value 100 to Integer Wrapper Class defined object obj. Java will automatically Box the value 100 into Integer Wrapper Class object.

Examples for AutoBoxing all types of primitive data types -

AutoBoxing different primitive data type values without creating object using new operator.

Integer obj1 = 100;
Byte obj2 = 9;
Short obj3 = 89;
Long obj4 = 123L;
Float obj5 = 12.5F;
Double obj6 = 123.456;
Character obj7 = 'a';
Boolean obj8 = true;

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'AutoBoxingDemo.java' with main( ) method in the existing Java Project 'Wrapper Classes' as shown below -



2. Implement AutoBoxing, by assigning all primitive data type values directly to the objects of the Wrapper Classes as shown below -


Observe that the errors are displayed in the above written AutoBoxing statements. As AutoBoxing feature was introduced from Java 5 version, but the Eclipse IDE Project by default uses the compiler version i.e. less than 4. We have to change the Compiler Compliance level of the Project by following the below steps to resolve the above errors.

3. Right Click on the Java Project 'Wrapper Classes' and select 'Properties' option

4. In 'Properties for Wrapper Classes' dialog, select 'Java Compiler' option on the left as shown below -


5. Change the compiler compliance level from the default 1.4 to the latest 1.7 and click on 'Apply' & 'OK' button as shown below -


6. Observe that all the errors got resolved as shown below -



7. Print the values of  primitive data types using the Wrapper Class defined objects as shown below -



8. Save & Run the Java Class 'AutoBoxingDemo.java' and observe that all the primitive data type values assigned to the Wrapper Class defined objects are printed as shown below -



In order to wrap the primitive data types values into objects, we can use AutoBoxing instead of manual Boxing. In AutoBoxing, new operator for creating an object for Wrapper Classes is not required. Instead we can directly assign primitive data type values to Wrapper Classes defined objects. Java will automatically Box the primitive values to the Wrapper Class objects.






Please comment below to feedback or ask questions.

Auto-UnBoxing will be explained in the next post.






No comments:

Blog Index