Boxing (computer programming)

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.

On the other hand, C# has no primitive wrapper classes, but allows boxing of any value type, returning a generic Object reference.

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.

As of J2SE 5.0, the compiler will accept the last line, and automatically transform it so that an Integer object is created to store the value 9.

Another example: J2SE 5.0 allows the programmer to treat a collection (such as a LinkedList) as if it contained int values instead of Integer objects.

This does not contradict what was said above: the collection still only contains references to dynamic objects, and it cannot list primitive types.

For example, the programmer can now write list.add(3) and think as if the int 3 were added to the list; but, the compiler will have actually transformed the line into list.add(new Integer(3)).

[4][5] Modern Object Pascal has yet another way to perform operations on simple types, close to boxing, called type helpers in FreePascal or record helpers in Delphi and FreePascal in Delphi mode.

The dialects mentioned are Object Pascal compile-to-native languages, and so miss some of the features that C# and Java can implement.