jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes two questions about wild card in Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "two questions about wild card in Generics" Watch "two questions about wild card in Generics" New topic
Author

two questions about wild card in Generics

Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
Question 1:
Collection<?> c = new ArrayList<String>();
c.add(new Object());

*******************************************************************

Question 2:
List<? super Integer> list = new ArrayList<Integer>();
list.add(1);

*******************************************************************


My question is why Question1 cannot be compiled. However, Question2 can be compiled and run.

I read a book and it says that all the situation using Wild Card is read-only.

Then, Why Question 2 still can be compiled?

Thanks a lot.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Song Gao:
Question 1:
Collection<?> c = new ArrayList<String>();
c.add(new Object());

*******************************************************************

Question 2:
List<? super Integer> list = new ArrayList<Integer>();
list.add(1);

*******************************************************************


My question is why Question1 cannot be compiled. However, Question2 can be compiled and run.

I read a book and it says that all the situation using Wild Card is read-only.

Then, Why Question 2 still can be compiled?

Thanks a lot.


Notice the type of the List is Integer or any class that is a superclass of Integer.

That means it could be a List<Integer>, a List<Number>, or List<Object>.

In any of these cases, it is safe to add an instance of an Integer into the list.
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
Question 1:
Collection<?> c = new ArrayList<String>();
c.add(new Object());

*******************************************************************

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
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: two questions about wild card in Generics
 
Similar Threads
Generic bounded assignment
Generics Doubt
Why this difference in List &lt;?&gt; and List
Wildcards and assignment
generics