Can all wildcards in Java be replaced by non-wildcard types?

Up vote 7 down vote favorite share g+ share fb share tw.

I can't find any example where wildcards can't be replaced by a generic. For example: public void dummy(List list); is equivalent to public void dummy(List list); or public List dummy2(List list); is equivalent to public List dummy(List list); So I don't undertand why wildcard was created as generics is already doing the job. Any ideas or comments?

Java generics wildcard link|improve this question edited Sep 6 '11 at 16:13 asked Sep 6 '11 at 8:47Olivier MARTIN583.

Nope, it is not always replaceable. List foo(); is not equivalent to List foo(); because you don't know the T when calling foo() (and you cannot know what List will foo() return. The same thing happens in your second example, too.

The demonstration of using wildcards can be found in this (my) answer.

I forgot that! Anyway, my remark concerning Angelika Lanker's FAQ stays true :-) – Riduidel Sep 6 '11 at 9:00 from caller's point of view, foo1() can only be assigned to List x = foo1(). Such usages can be replaced by List x = foo2().

Therefore foo1 can be replaced by foo2 (reverse isn't true). In the method body, foo2 can't really take advantage of T, so foo1 and foo2 impl are basically the same. – irreputable Sep 6 '11 at 19:48 @irreputable: The first point is true, foo2 can be used in place of foo1.

However, their implementation possibilities are substantially different, because foo1 can choose the type of list element. An example worth more than a thousand words: ideone.com/YKLum – jpalecek Sep 7 '11 at 11:26.

An easy answer is that, deeper level wildcards can't be replaced by a type variable void foo( List> arg ) is very different from void foo( List> arg) This is because wildcard capture conversion is only applied to 1st level wildcards. Let's talk about these. Due to extensive capture conversion, in most places, compiler treats wildcards as if they are type variables.

Therefore indeed programmer can replace wildcard with type variables in such places, a sort of manual capture conversion. Since a type variable created by compiler for capture conversion is not accessible to programmer, this has the restricting effect mentioned by @josefx. For example, compiler treats a List object as a List object; since W is internal to compiler, although it has a method add(W item), there's no way for programmer to invoke it, because he has no item of type W.

However, if programmer "manually" converts the wildcard to a type variable T, he can have an item with type T and invoke add(T item) on it. Another rather random case where wildcard can't be replaced type variable: class Base List foo() class Derived extends Base List foo() Here, foo() is overriden with a covariant return type, since List is a subtype of List This won't work if the wildcard is replaced with type variable.

There is a usage difference for your examples. Public List dummy2(List list); returns a list that can contain an unknown subtype of T so you can get objects of type T out of it but can't add to it. Example T = Number List l = new ArrayList(Arrays.

AsList(new Integer(1))); //Valid since the values are guaranteed to be a subtype of Number Number o = l. Get(0); //Invalid since we don't know that the valuetype of the list is Integer l. Add(new Integer(1)); So the wildcard can be used to make some operations invalid, this can be used by an API to restrict an interface.

Example: //Use to remove unliked numbers, thanks to the wildcard //it is impossible to add a Number @Override public void removeNumberCallback(List list){ list. Remove(13); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions