File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why this difference in List <?> and List

 
Satish Kota
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought List<?> and List are same. As ? wild character when not associated with super or extends can accept any generic type of Lists. But when i compile this i get an error



// this statement gives an error

Where as the following doesn't give any error. Why is this difference ?

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find the answers in Angelika Langer's Generic Faq
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm learning about Generics myself, so I cannot give you a definitive answer. What seems to help me is to forget about the actual list (ArrayList<Integer> ) and only consider the List<?> list1 part. So you give me one of those. It's a list of something but I do not know what kind of something. So how do I know that I can add a boxed int to it? It could be a list of Strings for all I know. To enforce stricter type checking Java 5.0 refuses to do this to a list specified by an unbounded wildcard.

If you just specify List list1 then the compiler says "this is just legacy stuff, I hope the programmer knows what he's upto. But I'd better warn him that he's in for trouble if that ain't a list of Integers."
[ December 12, 2006: Message edited by: Barry Gaunt ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic