Autoboxing and Unboxing
Automatic conversion rules.
Automatic Conversion
Autoboxing is Java automatically converting a primitive into its wrapper object. Unboxing is the reverse, turning a wrapper back into a primitive.
This happens silently so you can mix the two worlds without manual conversion calls.
Boxing in Action
Assigning an int to an Integer variable triggers autoboxing. The compiler inserts a call to Integer.valueOf for you.
public class Main {
public static void main(String[] args) {
int primitive = 42;
Integer boxed = primitive;
System.out.println(boxed);
}
}All lessons in this course
- Primitive vs Wrapper Types
- Autoboxing and Unboxing
- Parsing and Valueof
- Autoboxing Pitfalls