• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics and wildcards  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit confused with this piece of code, can you please explain why calling foo2() produces compile-time error?

 
Marshal
Posts: 56871
174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see if I can make sense out of that. But I think you would do better to go through the Java™ Tutorials.
Where do you have any wildcards? You are not saying ? super Foo anywhere, nor ? extends Foo which is what wildcards look like. You are saying <?> and that means, “type unknown”. Read List<?> as, “List of unknowns.”
Your first method call takes a List of unknown, so far so good. You can get things out of that list but you may be unable to put things in it, so you are on thin ice there.
Your third method isn't a generic method. It says it takes a list of lists of unknowns, and that is exactly the same as what you have declared your argument as.
Your second method is a generic method. You are saying it takes a List of Lists of a particular type. So the compiler can infer the type and put things into that List or take things out. What if you are writing
v.get(0).set(0, "Vladimir Mokrozub");
What is going to happen? Can you put Strings into that list? How do you know that list contains Strings at all? How do you know it contains a particular type at all? You don't. In fact, you can't rely on being able to put any particular type into the inner lists at all.
Remember when you use generics you are moving into a type‑safe environment. You are telling the compiler, “If my actions aren't completely type‑safe, don't compile this code.”
 
Saloon Keeper
Posts: 8110
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to read the method signature of foo2() as: "The caller will tell me what type T is, and then I'm going to accept a list of lists of that exact type". When you pass in a List<List<?>>, you're essentially first telling the method: "T could be ANY type, I don't know which". That's not valid, because the method expects an exact type. If your method should accept inexact types, you need to use wildcards in your method signature.
 
Vladimir Mokrozub
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I think I understand, but why this code does not compile:

List<? extends List<?>> means "list of lists of one unknown type", right? So basically it's the same as List<List<T>>?
 
Sheriff
Posts: 22958
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vladimir Mokrozub wrote:List<? extends List<?>> means "list of lists of one unknown type", right? So basically it's the same as List<List<T>>?



No, it doesn't mean that. It means List<X extends List<Y>> for some unknown types X and Y. And neither X nor Y have to be the same as T. So no, it's not the same as List<List<T>> and that's why you get the compiler error.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!