Boxing (computer programming): Difference between revisions

Content deleted Content added
m Sleepy bot
Alternate name
 
(11 intermediate revisions by 6 users not shown)
Line 1:
{{Short description|Programming language concept}}
{{Multiple issues|
{{redirect|Object type|another use|Class (computer programming)|the universal type|any type}}
{{More citations needed|date=August 2009}}
In [[computer science]], '''boxing''' (a.k.a. wrapping) is the transformation of placing a primitive type within an [[Object (computer science)|object]] so that the value can be used as a [[reference type|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.
{{Generalize|date=October 2009}}
}}
<!-- only talks of Java; there are complaints on the talk page about this -->
 
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 type|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==
Line 29 ⟶ 26:
</syntaxhighlight>
 
Compilers prior to 5.0 would not accept the last line. {{Java|Integer}} are reference objects, on the surface no different from {{Java|List}}, {{Java|Object}}, and so forth. To convert from an {{Java|int}} to an {{Java|Integer}}, one had to "manually" instantiate the Integer object. 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 {{Java|9}}.<ref>[https://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html oracle.com Java language guide entry on autoboxing]</ref> This means that, from J2SE 5.0 on, something like {{Java|1=Integer c = a + b}}, where {{Java|a}} and {{Java|b}} are {{Java|Integer}} themselves, will compile now - a and b are unboxed, the integer values summed up, and the result is autoboxed into a new {{Java|Integer}}, which is finally stored inside variable {{Java|c}}. The equality operators cannot be used this way, because the equality operators are already defined for reference types, for equality of the references; to test for equality of the value in a boxed type, one must still manually unbox them and compare the primitives, or use the {{Java|Objects.equals}} method.
 
Another example: J2SE 5.0 allows the programmer to treat a collection (such as a {{Java|LinkedList}}) as if it contained {{Java|int}} values instead of {{Java|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. It cannot be a {{Java|LinkedList<int>}}, but it must be a {{Java|LinkedList<Integer>}} instead. However, the compiler automatically transforms the code so that the list will "silently" receive objects, while the source code only mentions primitive values. For example, the programmer can now write {{Java|list.add(3)}} and think as if the {{Java|int}} {{Java|3}} were added to the list; but, the compiler will have actually transformed the line into {{Java|list.add(new Integer(3))}}.
Line 49 ⟶ 46:
 
C#:
<syntaxhighlight lang=CSharp"csharp">
int i = 42;
object o = i; // box
Line 57 ⟶ 54:
 
Java:
<syntaxhighlight lang=Java"java">
int i = 42;
Object o = i; // box
Line 63 ⟶ 60:
System.out.println(j); // unreachable line, author might have expected output "42"
</syntaxhighlight>
 
== Boxing in Rust ==
[[Rust (programming language)|Rust]] has the {{Code|Box}} type, which represents a uniquely owned, heap-allocated value:<ref>{{cite web |title=std::boxed - Rust |url=https://doc.rust-lang.org/std/boxed/index.html |website=doc.rust-lang.org |access-date=2 June 2025}}</ref>
<syntaxhighlight lang="rust">
let number: Box<i32> = Box::new(42);
</syntaxhighlight>
If the value needs to have shared ownership (e.g. between threads), one can use {{Code|Arc}}, which represents a reference-counted, heap-allocated value.<ref>{{cite web |title=Arc in std::sync - Rust |url=https://doc.rust-lang.org/std/sync/struct.Arc.html |website=doc.rust-lang.org |access-date=18 January 2025}}</ref><ref>{{cite web |title=Arc - Rust By Example |url=https://doc.rust-lang.org/rust-by-example/std/arc.html |website=doc.rust-lang.org |access-date=18 January 2025}}</ref>
 
==Type helpers==
Line 69 ⟶ 73:
But the feature is related to boxing.<br>
It allows the programmer to use constructs like
<syntaxhighlight lang="pascal">
{$ifdef fpc}{$mode delphi}{$endif}
uses sysutils; // this unit contains wraps for the simple types
Line 88 ⟶ 92:
[[Category:Data types]]
[[Category:Java (programming language)]]
[[Category:Programming language topicsconcepts]]