boxing
The automatic conversion between a primitive, such as int, and its wrapper object, such as Integer, which the compiler inserts for you.
In more detail
Assigning 42 to an Integer boxes it; assigning an Integer to an int unboxes it. The convenience has costs: boxing creates objects, and unboxing a null reference throws NullPointerException at runtime.
Example
Integer boxed = 42; int unboxed = boxed;Related terms
- wrapper class — A class, such as Integer for int, that wraps a primitive value in an object so it can be stored in collections or be null.
- primitive type — A basic built-in type, such as a whole number or true-or-false, whose values are held directly rather than as objects.
- NullPointerException — The unchecked exception thrown when code uses a null reference as if it pointed to an object.
Taught in
- Primitive And Reference Types — Modern Java: Virtual Threads & High-Throughput Services
- Promotion, Wrappers And The Math API — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Overloading And Varargs — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Lambdas, Functional Interfaces And Method References — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Stream Pipelines, Primitive Streams And Optional — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Lambdas And Streams — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Thread Safety: Locks And Atomics — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Modules And Concurrency — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
References
- Java Tutorials: Autoboxing and Unboxing
- dev.java: Autoboxing and Unboxing
- Java Language Specification, chapter 5: Conversions and Contexts