Boxing (computer programming): Difference between revisions

Content deleted Content added
Reverted good faith edits by Rtistique; Unsourced and makes little sense. (TW)
Boxing: wording
Line 6:
 
==Boxing==
'''Boxing''' is tothe placeprocess of placing a valueprimitive type within an object so that the valueprimitive 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>LinkedList</code> of <code>int</code>s, but the <code>LinkedList</code> class only lists references to dynamic objects — it cannot list primitive types, which are value types.
Line 19:
Autoboxing is the term for treating a value type as a reference type without any extra source code. The compiler automatically supplies the extra code needed to perform the [[type conversion]].
 
For example, J2SE 5.0 allows the programmer to create a <code>LinkedList</code> of <code>int</code>s. This does not contradict what was said above: the <code>LinkedList</code> still only lists references to dynamic objects, and it cannot list primitive types. But now, when Java expects a reference but receives a primitive type, it immediately converts that primitive type to a dynamic object. Note that the declaration List<int> is illegal in Java, but List<Integer> is not, and autoboxing will allow adding of primitive ints to the collection.
 
This action is called ''autoboxing'', because it is done automatically and implicitly instead of requiring the programmer to do so manually.
 
For example, in versions of Java prior to J2SE 5.0, the following code did not compile:
 
<source lang="java">
 
Integer i = new Integer(9);
Integer j = new Integer(13);