• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

two questions about wild card in Generics

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic