Google+

253. Implementing AutoBoxing and Auto-UnBoxing in methods








In method calling, AutoBoxing is the process by which a primitive data type is automatically boxed into its equivalent type wrapper whenever an object of that type need to be passed to the method. Lets understand that in the below example -

Example -

public static void main(String args[]) {

       methodOne(100);

}

public static void methodOne(Integer obj) {

         System.out.println(obj);

}

In the above example, we have called the methodOne( ) by passing primitive data type value 100 and the Integer Wrapper Class object parameter inside the methodOne( ) receives the primitive data type value 100. Hence the passed primitive data type value 100 is autoboxed to Integer Wrapper Class object x.

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'AutoBoxingInMethods.java' with main( ) method as shown below -



2. Create another method say 'methodOne( )' with Integer Wrapper Class object as parameter as shown below -


3. Call methodOne( ) by passing integer primitive data type value 100 from main( ) method as shown below -


4. Print the Integer Wrapper Class object parameter obj inside the methodOne( ) method as shown below -


5. Save & Run the Java Class 'AutoBoxingInMethods.java' and observe that the Integer Wrapper Class object obj is printed in the output as shown below -


Hence we have implemented the AutoBoxing concept in the method calling.

In method returning, Auto-UnBoxing is the process by which an Integer Wrapper Class object obj is returned to the calling method which accepts the values of int primitive data type as shown in the below example -

public static void main(String args[]) {

       int var = methodOne(100);

      System.out.println(int primitive data type variable var is "+var);

}

public static Integer void methodOne(Integer obj) {

         System.out.println(obj);

         return obj;

}

In the above example, we have returned the Wrapper Class object obj to the calling method methodOne(100) using return keyword and by adding the return type Integer in the method declaration and the Integer Wrapper Class object parameter is received by the methodOne(100) calling method and assign received returned object obj to int primitive data type variable var. Hence the returned Wrapper Class Object obj is auto-unboxed to int primitive data type variable var. 

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, open the existing Java Class 'AutoBoxingInMethods.java' of the Java Project 'Wrapper Classes' as shown below - 


2. Change the return type of the methodOne( ) method by replacing void with Integer as shown below -


3. Now return the object obj using return keyword inside the methodOne( ) method as shown below -


4. Now assign the object obj received by the calling method in main( ) method to the int primitive data type variable var as shown below -


5. Print the int primitive data type variable var as shown below -


6. Save & Run the Java Class 'AutoBoxingInMethods.java' and observe that the auto-boxed int primitive data type variable var is printed in the output as shown below -


Hence we have implemented the Auto-UnBoxing concept by returning the Wrapper Class to int primitive data type variable.







Please comment below to feedback or ask questions.

Implementing AutoBoxing and Auto-UnBoxing in expressions will be explained in the next post.








No comments: