Wednesday, September 12, 2012

AutoBoxing & AutoUnBoxing

AutoBoxing & AutoUnBoxing in Java

Here we talk about Java AutoBoxing & AutoUnBoxing feature.Which comes into picture after JDK 1.5.
When we talk about variables in java,we  have two types of variables first one is primitive type & another one is referenceType.

Primitive: int,short,boolean etc
Reference: Integer,Short,Boolean etc


In java we have eight primitive type variables & for each of this primitive type we have corresponding Reference type varibale.These reference type variables are subtype of class Object & should be set to null during initilization.

                        


          
Boxing: Conversion of primitive type into corresponding reference type.
 So if a expression x of type "int" appears where an "Integer" is expected then java automatically converts it into new Integer(x).Also maintain some catching for frequently occuring values.This is called as autoboxing.


Unboxing: Conversion of reference type into corresoponding primitive type.
Similarly if an expression x is of type "Integer" where an "int is expecte then java automatically converts it into x.intValue().This is called as autounboxing.





Lets start with an example:


List<Integer> abc =  new ArrayList<Integer>();
abc.add(20);
int c = abc.get(0);

This is similar as:
List<Integer> abc = new ArrayList<Integer>();
abc.add(Integer.valueOf(20));
int c = abc.get(0).intValue();

So the method call Integer.valueOf(20) is similar to new Integer(20).But its cache some values for improved performance.

Coding Practices:

Example 1:

public static int multiply(List<Integer> intList){
int result = 0;
for(int num: intList){
result * = num;}
return result;
}

Now consider another similar example:


public static Integer multiplyInteger(List<Integer> intList){
Integer result = 0;
for(Integer num: intList){
result * = num; }
return result;
}

Both of these method gave the same result.what is the difference between these two methods?????????
Second method is around 60% slower then the first method.

What is the reason behind this????

Each interation of Foreach loop have extra overhead over here.It first convert the num & result into the int value then perform the multiplication operation and then again convert it into Integer.Although java autoboxing & unboxing is working fine over here but still there is computational time  difference between these two methods.So avoid these type of methods.

Example 2:

Taking previous example into consideration.Let suppose we have values 10,20,30

What will be the result of :

List<Integer> abc = Arrays.asList(10,20,30);
result 1:  multiply(abc) == multiplyInteger(abc);  //result 1: true
result 2: multiplyInteger(abc) == multiplyInteger(abc); //I will never suggest to use == //result 2: false

AutoBoxing & AutoUnboxing concept will not work in this case.Both of them are treated as different objects.

Example 3:


List<Integer> abc = Arrays.asList(1,2,3);

result 1:  multiply(abc) == multiplyInteger(abc); //result 1: true
result 2: multiplyInteger(abc) == multiplyInteger(abc);//I will never suggest to use == //result 2: true


 Here It will work for smaller values like till -127 to 128 for int & short .There are some caching that is involved behind all this.But the main concept is never use == for comparing reference type Objects.






No comments:

Post a Comment