Wildcard (Java): Difference between revisions

Content deleted Content added
m top: Add link to Type safety.
Add in-line citation
Line 98:
In order to be able to do both add objects of type <code>MyClass</code> to the list and iterate over it using a variable of type <code>MyClass</code>, a <code>List<MyClass></code> is needed, which is the only type of <code>List</code> that is both <code>List<? extends MyClass></code> and <code>List<? super MyClass></code>.<ref>[[Java syntax|Java syntax(Generics)]]</ref>
 
The mnemonics PECS (Producer Extends, Consumer Super) from the book '''Effective Java''' by [[Joshua Bloch]] gives an easy way to remember when to use wildcards (corresponding to Covariance and Contravariance) in Java.{{sfn|Bloch|2018|loc=Chapter §5 Item 31: Use bounded wildcards to increase API flexibility|pp=139-145}}
 
==See also==
Line 106:
* [[Generics in Java#Type wildcards]] section explains lower and upper wildcard bounds
 
== ReferencesCitations ==
 
{{Reflist}}
 
== References ==
*{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=[[Addison-Wesley]] | edition=third | isbn=978-0134685991| year=2018}}
* The Java Language Specification, Third Edition (Sun), {{ISBN|978-0-321-24678-3}} http://java.sun.com/docs/books/jls/
* Java Tutorials, Lesson Generics http://download.oracle.com/javase/tutorial/java/generics/index.html