O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required