It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why this difference in List <?> and List 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 "Why this difference in List &lt;?&gt; and List" Watch "Why this difference in List &lt;?&gt; and List" New topic
Author

Why this difference in List &lt;?&gt; and List

Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
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 ?



SCJP 5.0 77%
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You will find the answers in Angelika Langer's Generic Faq


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why this difference in List &lt;?&gt; and List