0 votes
in JAVA by
What are autoboxing and unboxing? When does it occur?

1 Answer

0 votes
by

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

+1 vote
asked Jan 10, 2021 in JAVA by rajeshsharma
0 votes
asked May 3, 2021 in JAVA by Robindeniel
...