Boxing is when a value type is wrapped in a reference-type wrapper for the purposes of using it when polymorphism (conversion to Object or an interface) is required.
In Java and some similar languages there is a differentiation between primitives (or value types) and reference types. The primitive types work as simple values and, when used as a parameter to methods, do not change the original operand. Reference types on the other hand can be modified when passed into methods and have associated classes that define objects of that type (whereas primitives are more "built-in"). When viewed from the perspective of language such as C or C++, instances of reference types can be thought of as pointers that are automatically de-referenced.
Boxing occurs when a primitive value type is used in place of a corresponding reference type. In Java, for example, if a value of type int
is used where an Integer
is expected, the compiler will automatically wrap the value in an Integer
object.