Wildcard (Java): Difference between revisions

Content deleted Content added
Polemyx (talk | contribs)
m Bounded Wildcards: corrected non-standard use of prepositions, and lack of conjunction between phrases
Polemyx (talk | contribs)
m Object Creation with Wildcard: corrected punctuation; added indefinite articles required by standard English
Line 43:
 
== Object Creation with Wildcard ==
No objects may be created with a wildcard type parameter: <code>('''new''' Generic<?>()</code> is forbidden because <code>Generic<?></code> is abstract). In practice, this is unnecessary because if one wanted to create an object that was assignable to a variable of type <code>Generic<?></code>, one could simply use any arbitrary type (that falls within the constraints of the wildcard, if any) as the type parameter.
 
On the other hand, an array object that is an array of a parameterized type may be created only by an unconstrained (i.e. with a wildcard type parameter) type (and by no other instantiations) as the component type: <code>'''new''' Generic<?>[20]</code> is correct, while <code>'''new''' Generic<UpperBound>[20]</code> is prohibited.
 
An example of using a wildcard in List's instantiation is contained in the article [[Generics in Java]].
 
== Example: Lists ==