Wildcard (Java): Difference between revisions

Content deleted Content added
rewrite/expand
Line 5:
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 with 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 instantiationsparameterizarions of the generic type. ItThis means,this noallows objects of this type may<code>Generic<Supertype></code> beand created,<code>Generic<Subtype></code> onlyto variables.be Thesafely usageassigned of suchto a variable isor tomethod refer to instantiationsparameter of type <code>Generic<?></code> with any actual type parameter.
 
== Wildcard as parameter type ==