0 votes
in JAVA by (30.6k points)
What are autoboxing and unboxing? When does it occur?

1 Answer

0 votes
by (30.6k points)

The autoboxing is the process of converting primitive data type to the corresponding wrapper class object, eg., int to Integer. The unboxing is the process of converting wrapper class object to primitive data type. For eg., integer to int. Unboxing and autoboxing occur automatically in Java. However, we can externally convert one into another by using the methods like valueOf() or xxxValue().

It can occur whenever a wrapper class object is expected, and primitive data type is provided or vice versa.

  • Adding primitive types into Collection like ArrayList in Java.
  • Creating an instance of parameterized classes ,e.g., ThreadLocal which expect Type.
  • Java automatically converts primitive to object whenever one is required and another is provided in the method calling.
  • When a primitive type is assigned to an object type.

Related questions

0 votes
asked Aug 21, 2022 in JAVA by sharadyadav1986 (30.6k points)