What is Autoboxing in Java
Autoboxing and unboxing is introduced in Java 1.5 to automatically convert primitive type into boxed primitive( Object or Wrapper class). autoboxing allows you to use primitive and object type interchangeably in Java on many places like assignment, method invocation etc. If you have been using Collections like HashMap or ArrayList 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, Double and Boolean helps for converting primitive to Object but that clutter the code. With the introduction of autoboxing and unboxing in Java this primitive to object conversion happens automatically by Java compiler which makes code more readable. But autoboxing 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 autoboxing in Java code occurs and when 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 autoboxing and unboxing in Java ? When autoboxing and unboxing occurs in Java? and things to remember while dealing with primitives and objects in Java with code examples.