Content deleted Content added
Reverted good faith edits by Rtistique; Unsourced and makes little sense. (TW) |
→Boxing: wording |
||
Line 6:
==Boxing==
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.
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);
|