Question 2: List<? super Integer> list = new ArrayList<Integer>(); list.add(1); in question '1' you are defining the collection to take any type. but the type you define after the = sign is String. so you can only add Strings to such a collection, but you have tried to add a object type which is not allowed. in question two however when you define it as super then you can add anything that is an Integer or a super class of Integer.
You have to use the word super with wild cards if you want to add anything to that list. in the previous example you didnt that too was a fact that you got a compile error.
hope this helps
if who i am is what i have, and what i have is lost, then who am i?<br /> <br />SCJP 5.0<br />SCWCD 1.4<br />SCBCD preparing
Joined: Feb 07, 2005
I have a disagreement with what you said about Question 1. Because the type of the left is a Collection containing anything, it is not safe to add any object to it. Even though the actual object is an ArrayList containing String, you cannot add a String to the collection because the reference type is Collection<?>.
Steven Gao Song
Joined: Oct 02, 2006
Yes. I see.
If you want to add any element to Collection specified by wild card, you must provide a lower bound. e.g. List<? super ClassName>
The Book: "Java 1.5 Tiger: A Developer's Notebook" doesn't explain it very well.
subject: two questions about wild card in Generics