Sobes.tech
Junior

What is autoboxing in Java?

sobes.tech AI

Answer from AI

Autoboxing is the automatic conversion of primitive data types to their corresponding wrapper classes by the Java compiler.

For example:

Integer i = 10; // Autoboxing of int (primitive) to Integer (object)

Unboxing is the reverse process, automatically extracting the primitive value from a wrapper class object.

For example:

int j = new Integer(5); // Unboxing of Integer (object) to int (primitive)

It is used for convenience when working with collections (e.g., ArrayList<Integer>) or in situations where an object wrapper for a primitive is required. It can lead to a "NullPointerException" during unboxing if the wrapper object is null. It may also slightly affect performance due to object creation.