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]]:
==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
To circumvent this,
On the other hand, [[C Sharp (programming language)|C#]] has no primitive wrapper classes, but allows boxing of any value type, returning a generic
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=
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.
Another example: J2SE 5.0 allows the programmer to treat a collection (such as a
===Unboxing===
Line 37:
For example, in versions of Java prior to J2SE 5.0, the following code did not compile:
<source lang=
Integer k = new Integer(4);
int l = k.intValue(); // always OK
Line 48:
C#:
<source lang=
int i = 42;
object o = i; // box
int j = o; // unbox (ERROR)
Console.Writeline(j); // outputs "42"
</source>
Java:
<source lang=
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]]
|