Autoboxing
As wonderful as the primitive wrappers are, they often mean we end up creating a lot of objects (say with valueOf(int i)), passing them into a list, and then putting them back into their primitive values again with intValue()) when we drag them out of the list.
Autoboxing is a feature new to Java 1.5 that takes care of this for us. Here's how it works in a nutshell. Create a primitive type. Pass it as an argument to a method that expects an Object. It magically works. The JVM converts the primitive to its corresponding wrapper on-the-fly for you.
Autoboxing wouldn't be complete without auto-unboxing. As you might guess, this means that when you ask for a primitive directly from an Object wrapper, ye shall receive.
Because this concept ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access