Auto boxing and Unboxing in Java – Be careful while using
Blog :TIBCO RV FIX PROTOCOL JAVA TUTORIAL
Date: 7/18/2012 12:55:00 PM
Read more »
Auto boxing in Java
Auto boxing and un-boxing is introduced in Java 1.5 to automatically convert primitive type into boxed primitive( Object or Wrapper class) in Java. If you have been using Collections like HashMap or ArrayList before Java 1.5 then you are familiar with the issue that you can not directly put primitives into Collections, instead you first need to convert them in to Object only then you can put them into Collections. Wrapper class like Integer, Double and Boolean helps for converting primitive to Object but that clutter the code. With the introduction of auto boxing and unboxing in Java this primitive to object conversion happens automatically by Java compiler which makes code more readable. But auto boxing and unboxing comes with certain caveats which needs to be understood before using them in production code and it becomes even more important because they are automatic and can create subtle bugs if you are not sure when auto boxing occurs and when auto unboxing happens. This is my fifth article on features introduced in Java 5 after my post on Java Enum, How Generics works in Java and varargs example. In this Java tutorial we will see: What is auto boxing and unboxing? When auto boxing and unboxing occurs in Java? and things to remember while dealing with primitives and objects in Java with code examples.