Sobes.tech
Junior — Middle

Kas saaksite selgitada, mida tähendab automaatne primitiivsete tüüpide pakendamine ja lahtipakkimine Java's?

sobes.tech AI

Vastus AI-lt

Autoboxing and unboxing in Java are mechanisms that allow automatic conversion of primitive data types (int, boolean, double, etc.) to their corresponding wrapper objects (Integer, Boolean, Double, etc.) and vice versa.

For example, previously it was necessary to explicitly create an object:

Integer x = Integer.valueOf(5); // boxing
int y = x.intValue(); // unboxing

With autoboxing, it can be written more simply:

Integer x = 5; // int is automatically boxed into Integer
int y = x;    // Integer is automatically unboxed into int

This makes working with collections and other classes that operate on objects easier, rather than primitive types.