Junior — Middle
Is it possible to add primitive type values directly to an ArrayList without using their wrappers?
sobes.tech AI
Answer from AI
In Java, you cannot directly add primitive types (int, boolean, double, etc.) to collections such as ArrayList because collections work with objects, not primitives. However, since Java 5, autoboxing has been introduced, which automatically converts primitives to their corresponding wrapper objects (e.g., int -> Integer) when adding to a collection.
Example:
ArrayList<Integer> list = new ArrayList<>();
list.add(10); // int is automatically boxed into Integer
Thus, you write as if you are adding a primitive, but in reality, it is boxed into an object wrapper.