Monday, May 7, 2012

auto-boxing, boxing, unboxing



  • Lesson3 Boxing & Unboxing

http://www.youtube.com/watch?v=ujbeM5MD84M&feature=autoplay&list=ULr_jhKrpDD5c&playnext=1



  • java 09 - Wrappers and Boxing

http://www.youtube.com/watch?v=KUdvCjLqP_0
wrapper class wraps primitive type into an object





  • Java Tutorial 7.1 Autoboxing Part 1/3

http://www.youtube.com/watch?v=JPIN1vZBM8M&feature=relmfu


  • Java Tutorial 7.2 Autoboxing Part 2/3

http://www.youtube.com/watch?v=uGLoHyjURtc&feature=relmfu


  • java Tutorial 7.3 Autoboxing Part 3/3

http://www.youtube.com/watch?v=jqKn_dCBU20&list=UU77mSrM47r2NgFoxGSl4DXw&index=154&feature=plpp_vide



  • Autoboxing

Autoboxing and unboxing is introduced in Java 1.5 to automatically convert primitive type into boxed primitive( Object or Wrapper class).
before Java 1.5 then you are familiar with the issues like you can not directly put primitives into Collections, instead you first need to convert them into Object only then only you can put them into Collections. Wrapper class like Integer
With the introduction of autoboxing and unboxing in Java this primitive to object conversion happens automatically by Java compiler which makes code more readable


it becomes even more important because they are automatic and can create subtle bugs if you are not sure when autoboxing  in Java code occurs and when unboxing happens.

When Java automatically converts a primitive type like int into corresponding wrapper class object e.g. Integer than its called autoboxing  because primitive is boxed into wrapper class while in opposite case is called unboxing, where an Integer object is converted into primitive int.


All primitive types e.g. byte, short, char, int, long, float, double and boolean has corresponding wrapper class e.g. Byte, Short, Integer, Character etc and participate in autoboxing and unboxing.


During autoboxing boolean is converted to Boolean, byte to Byte, char converted to Character, float changes to Float, int goes to Integer, long goes to Long and short converts to Short, while in unboxing opposite happens like Float to float.

for example In method invocation where an object argument is expected,  if you pass primitive, Java automatically converts primitive into equal value Object.

Autoboxing mainly occur in two places one is during assignment and other is during method invocation


One of the danger of autoboxing is throw away object which gets created if autoboxing occurs in a loop. Here is an example of how unnecessary object can slow down your application


Integer sum = 0;
 for(int i=1000; i<5000 i="i" p="p">   sum+=i;
 }

In this code sum+=i will expand as sum = sum + i and since + operator is not applicable to Integer object it will trigger unboxing of sum Integer object and then autoboxing of result which will be stored in sum which is Integer as shown below :

sum = sum.intValue() + i;
Integer sum = new Integer(result);    
   
here since sum is Integer, it will create around 4000 unnecessary Intger object which are just throw away and if this happens on large scale has It potential to slow down system with frequent GC for arithmetic calculation always prefer primitive over boxed primitive and look for unintentional autoboxing in Java

autoboxing is error prone e.g. equality operator "==". Since equality operator can be applied on both primitive and Objects it leads to confusion and can cause subtle issues

When you compare two object using "==" operator it compares object's identity and not value and also no auto boxing occur.
its not best practice to use  equality operator to compare Objects, use equals method instead

if we compare one primitive with another object than unboxing of object is occur which could throw NullPointerException if object is null e.g.

Since autoboxing creates unnecessary object and if that goes beyond a limit usually outside the range of cached value it can potentially slow your program by frequently causing garbage collection.



http://javarevisited.blogspot.com/2012/07/auto-boxing-and-unboxing-in-java-be.html#ixzz22r1HOohK





7 comments: