Boxing (computer programming): Difference between revisions

Content deleted Content added
Unboxing: fix error
m Improved formatting & made minor corrections
Line 6:
In [[computer science]], an '''object type''' (a.k.a. '''wrapping object''') is a [[datatype]] which is used in [[object-oriented programming]] to [[wrapper pattern|wrap]] a non-object type to make it look like a [[Reference type|dynamic]] [[object (computer science)|object]].{{Citation needed|date=August 2009}}
 
Some [[object-oriented programming language]]s make a distinction between [[Reference type|reference]] and [[value type]]s, often referred to as objects and non-objects on platforms where complex value types don't exist, for reasons such as runtime efficiency and syntax or semantic issues. For example, [[Java (programming language)|Java]] has [[primitive wrapper class]]es corresponding to each [[primitive type]]: <code>{{Java|Integer</code>}} and <code>{{Java|int</code>}}, <code>{{Java|Character</code>}} and <code>{{Java|char</code>}}, <code>{{Java|Float</code>}} and <code>{{Java|float</code>}}, etc. Languages like [[C++]] have little or no notion of reference type; thus, the use of object type is of little interest.
 
==Boxing==
Boxing is the process of placing a primitive type within an object so that the primitive can be used as a reference object. For example, [[List (computing)|lists]] may have certain [[Method (computer science)|methods]] which [[array data type|arrays]] might not, but the list might also require that all of its members be dynamic objects. In this case, the added functionality of the list might be unavailable to a simple array of numbers.
For a more concrete example, in Java, a {{Javadoc:SE|java/util|LinkedList}} can change its size, but an array must have a fixed size. One might desire to have a <code>{{Java|LinkedList</code>}} of <code>{{Java|int</code>}}s, but the <code>{{Java|LinkedList</code>}} class only lists references to dynamic objects — it cannot list primitive types, which are value types.
 
To circumvent this, <code>{{Java|int</code>}}s can be boxed into <code>{{Java|Integer</code>}}s, which are dynamic objects, and then added to a <code>{{Java|LinkedList</code>}} of <code>{{Java|Integer</code>}}s. (Using [[generic programming|generic]] parameterized types introduced in [[Java Platform, Standard Edition|J2SE]] 5.0, this type is represented as <code>{{Java|LinkedList<Integer></code>}}.)
On the other hand, [[C Sharp (programming language)|C#]] has no primitive wrapper classes, but allows boxing of any value type, returning a generic <code>{{C sharp|Object</code>}} reference.
 
The boxed object is always a copy of the value object, and is usually [[Immutable object|immutable]]. Unboxing the object also returns a copy of the stored value. Note that repeated boxing and unboxing of objects can have a severe performance impact, since it [[Dynamic memory allocation|dynamically allocates]] new objects and then makes them eligible for [[Garbage collection (computer science)|Garbage collection]].
Line 22:
For example, in versions of Java prior to J2SE 5.0, the following code did not compile:
 
<source lang="java"Java>
Integer i = new Integer(9);
Integer l = 9; // error in versions prior to 5.0!
</source>
 
Compilers prior to 5.0 would not accept the last line. <code>{{Java|Integer</code>}}s are reference objects, on the surface no different from <code>{{Java|List</code>}}, <code>{{Java|Object</code>}}, and so forth. To convert from an <tt>int</tt> to an 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 the value <tt>9</tt><ref>[http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html java.sun.com Java language guide entry on autoboxing]</ref>. This means that, from J2SE 5.0 on, something like <tt>Integer c = a + b;</tt>, where <tt>a</tt> and <tt>b</tt> are Integers themselves, will compile now, because they are unboxed, the integer values summed up, and the result is autoboxed into a new Integer, which is finally stored inside variable <tt>c</tt>.
 
 
Another example: J2SE 5.0 allows the programmer to treat a collection (such as a <code>{{Java|LinkedList</code>}}) as if it contained <tt>int</tt> values instead of <tt>Integer</tt> 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 can '''not''' be a <tt>List&lt;int></tt>, but it must be a <tt>List&lt;Integer></tt> 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 <tt>list.add(3);</tt> and think as if the int <tt>3</tt> were added to the list; but, the compiler will have actually transformed the line into <tt>list.add(new Integer(3))</tt>.<br/>
 
===Unboxing===
Line 37:
For example, in versions of Java prior to J2SE 5.0, the following code did not compile:
 
<source lang="java"Java>
Integer k = new Integer(4);
int l = k.intValue(); // always OK
Line 48:
 
C#:
<source lang="csharp"CSharp>
int i = 42;
object o = i; // box
int j = o; // unbox (ERROR)
Console.Writeline(j); // outputs "42"
</source>
 
Java:
<source lang="java"Java>
int i = 42;
Object o = i; // box
int j = o; // unbox (ERROR)
System.out.println(j); // outputs "42"
</source>
 
Line 66:
{{reflist}}
 
<!--Categories-->
{{DEFAULTSORT:Object Type (Object-Oriented Programming)}}
[[Category:Data types]]
Line 71 ⟶ 72:
[[Category:Programming language topics]]
 
<!--Interwikis-->
[[nl:Boxing]]
[[ja:ボックス化]]
[[nl:Boxing]]