Why is primitive type int changed to object Integer automatically when i put primitve type int to ArrayList in java?
This is a java language feature that was introduced with java 1.5. It's called autoboxing.
Roughly spoken, it converts between java primitive types to their corresponding wrapper class types. The compiler detects when inboxing (primitive-to-wrapper) or outboxing (wrapper-to-primitive) is needed (and possible) and expands the expression the correct byte code.
So, behind the scenes, an instance of Integer
is added to the list when you add an int
.