The autoboxing and unboxing Java features let you write cleaner and easier-to-read code by allowing you to use either primitive data types or their corresponding wrapper objects. Java then automatically makes the needed conversions in code at runtime.

Conversion Applied when
Autoboxing Primitive → Wrapper A primitive value is:

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class
  • Assigned to a variable of the corresponding wrapper class.
Unboxing Wrapper → Primitive A wrapper object is:

  • Passed as a parameter to a method that expects a value of the corresponding primitive type
  • Assigned to a variable of the corresponding primitive type
  • Used in expressions with operators

Autoboxing and unboxing both work for all primitive data types, including char and boolean.

 


Leave a Reply