Wildcard (Java): Difference between revisions

Content deleted Content added
Polemyx (talk | contribs)
m Covariance for generic types: fixed incorrect preposition; added required indefinite article
Line 3:
== Covariance for generic types ==
 
Unlike arrays (which are [[Covariance and contravariance (computer science)#Covariant arrays in Java and C#|covariant]] in Java), different instantiations of a generic type are not compatible towith each other, not even explicitly: With the declaration <code>Generic<Supertype> superGeneric; Generic<Subtype> subGeneric;</code> the compiler would report a conversion error for both castings <code>(Generic<Subtype>)superGeneric</code> and <code>(Generic<Supertype>)subGeneric</code>.
 
This incompatibility may be softened by the wildcard if <code>?</code> is used as an actual type parameter: <code>Generic<?></code> is the abstract supertype for all instantiations of the generic type. It means, no objects of this type may be created, only variables. The usage of such a variable is to refer to instantiations of <code>Generic</code> with any actual type parameter.
 
== Wildcard as parameter type ==