Hubbry Logo
search
logo

Boxing (computer programming)

logo
Community Hub0 Subscribers
Write something...
Be the first to start a discussion here.
Be the first to start a discussion here.
See all
Boxing (computer programming)

In computer science, boxing (a.k.a. wrapping) is the transformation of placing a primitive type within an object so that the value can be used as a reference. Unboxing is the reverse transformation of extracting the primitive value from its wrapper object. Autoboxing is the term for automatically applying boxing and/or unboxing transformations as needed.

Boxing's most prominent use is in Java where there is a distinction between reference and value types for reasons such as runtime efficiency and syntax and semantic issues. In Java, a LinkedList<T> must be such that T is an instance of Object. One might desire to have a LinkedList<int>, but this is not directly possible. Instead Java defines primitive wrapper classes corresponding to each primitive type: Integer and int, Character and char, Float and float, etc. Using generic parameterized types introduced in J2SE 5.0, one can then define a LinkedList<Integer> and insert int values into the list by boxing them as Integer objects.

On the other hand, C# has no primitive wrapper classes, but allows boxing of any value type, returning a generic Object reference. In Objective-C, any primitive value can be prefixed by a @ to make an NSNumber out of it (e.g. @123 or @(123)). This allows for adding them in any of the standard collections, such as an NSArray.

Haskell has little or no notion of reference type, but still uses the term "boxed" for the runtime system's uniform pointer-to-tagged union representation.

The boxed object is always a copy of the value object, and is usually immutable. Unboxing the object also returns a copy of the stored value. Repeated boxing and unboxing of objects can have a severe performance impact, because boxing dynamically allocates new objects and unboxing (if the boxed value is no longer used) then makes them eligible for garbage collection. However, modern garbage collectors such as the default Java HotSpot garbage collector can more efficiently collect short-lived objects, so if the boxed objects are short-lived, the performance impact may not be severe.

In some languages, there is a direct equivalence between an unboxed primitive type and a reference to an immutable, boxed object type. In fact, it is possible to substitute all the primitive types in a program with boxed object types. Whereas assignment from one primitive to another will copy its value, assignment from one reference to a boxed object to another will copy the reference value to refer to the same object as the first reference. However, this will not cause any problems, because the objects are immutable, so there is semantically no real difference between two references to the same object or to different objects (unless you look at physical equality). For all operations other than assignment, such as arithmetic, comparison, and logical operators, one can unbox the boxed type, perform the operation, and re-box the result as needed. Thus, it is possible to not store primitive types at all.

Autoboxing is the term for getting a reference type out of a value type just through type conversion (either implicit or explicit). The compiler automatically supplies the extra source code that creates the object.

For example, in versions of Java prior to J2SE 5.0, the following code did not compile:

See all
User Avatar
No comments yet.