• Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble with <?>  RSS feed

 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code:



gives me:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method add(capture#1-of ?) in the type ArrayList<capture#1-of ?> is not applicable for the arguments (String)

ArrayList<? extends String> also gives the same error, but ArrayList<? super String> works. Could someone explain that exception please?



 
Mitch Aman
Greenhorn
Posts: 9
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem lies with

Although it may compile, myList does not know what type of list it is.
In generics, you must fill all sets of angle brackets with at least one data type.


Would give you what you are looking for.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unresolved compilation problem? Does that mean you are using Eclipse? go back to the editing window, where there is a red mark against the line, and hover your mouse on it. That will give you a description of the error.
Have you been through the Java Tutorials? There are two sections about generics: 1 2.
If you use <? super String>, there are only two possible classes, viz String and Object. If you use a wildcard which is a subclass of something, then you probably cannot add anything to that List in case it is the wrong class.

I am sure somebody else would be able to explain it much better than me.
 
Andrea Binello
Ranch Hand
Posts: 62
5
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:ArrayList<? extends String> also gives the same error, but ArrayList<? super String> works. Could someone explain that exception please?

First of all, the ? itself is called the "unbounded wildcard". The ? along with bounds using extends/super keywords has been added to generics to allow a sort of "polymorphism" to alleviate (in part) the fact that generics are invariant (a List<String> is not a List<Object> while String[] is-a Object[] ).

A List<?> (or subtypes of List) doesn't mean a list where you can put any type you want. It means a list whose parametrization is unknown. Given:
ArrayList<?> myList = ........

You could assign to the variable an ArrayList<String> or an ArrayList<Date> but through the variable myList you can't know the real parametrization.
And if you don't know the real parametrization, you can only do 2 things with list elements:
- you can only extract an element as java.lang.Object.
- you can only put a null literal (because null is the subtype of all types).

ArrayList<? super String> is only a little different. Here you can say: "ok, I still don't know the real parametrization but ... now I am sure of one thing: it is something that is String or higher in the hierarchy". It could be an ArrayList<String> or an ArrayList<Object> for example ... you don't known just from the variable. But it is certain that you can insert a String because every list that can be legally assigned can contain a String.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote: . . . I am sure somebody else would be able to explain it much better than me.
And somebody did. Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!