160

I am reading about generic methods from OracleDocGenericMethod. I am pretty confused about the comparison when it says when to use wild-card and when to use generic methods. Quoting from the document.

interface Collection<E> {
    public boolean containsAll(Collection<?> c);
    public boolean addAll(Collection<? extends E> c);
}

We could have used generic methods here instead:

interface Collection<E> {
    public <T> boolean containsAll(Collection<T> c);
    public <T extends E> boolean addAll(Collection<T> c);
    // Hey, type variables can have bounds too!
}

[…] This tells us that the type argument is being used for polymorphism; its only effect is to allow a variety of actual argument types to be used at different invocation sites. If that is the case, one should use wildcards. Wildcards are designed to support flexible subtyping, which is what we're trying to express here.

Don't we think wild card like (Collection<? extends E> c); is also supporting kind of polymorphism? Then why generic method usage is considered not good in this?

Continuing ahead, it states,

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used.

What does this mean?

They have presented the example

class Collections {
    public static <T> void copy(List<T> dest, List<? extends T> src) {
    ...
}

[…]

We could have written the signature for this method another way, without using wildcards at all:

class Collections {
    public static <T, S extends T> void copy(List<T> dest, List<S> src) {
    ...
}

The document discourages the second declaration and promotes usage of first syntax? What's the difference between the first and second declaration? Both seems to be doing the same thing?

Can someone put light on this area.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
benz
  • 4,561
  • 7
  • 37
  • 68

9 Answers9

226

There are certain places, where wildcards, and type parameters do the same thing. But there are also certain places, where you have to use type parameters.

  1. If you want to enforce some relationship on the different types of method arguments, you can't do that with wildcards, you have to use type parameters.

Taking your method as example, suppose you want to ensure that the src and dest list passed to copy() method should be of same parameterized type, you can do it with type parameters like so:

public static <T extends Number> void copy(List<T> dest, List<T> src)

Here, you are ensured that both dest and src have same parameterized type for List. So, it's safe to copy elements from src to dest.

But, if you go on to change the method to use wildcard:

public static void copy(List<? extends Number> dest, List<? extends Number> src)

it won't work as expected. In 2nd case, you can pass List<Integer> and List<Float> as dest and src. So, moving elements from src to dest wouldn't be type safe anymore. If you don't need such kind of relation, then you are free not to use type parameters at all.

Some other difference between using wildcards and type parameters are:

  • If you have only one parameterized type argument, then you can use wildcard, although type parameter will also work.
  • Type parameters support multiple bounds, wildcards don't.
  • Wildcards support both upper and lower bounds, type parameters just support upper bounds. So, if you want to define a method that takes a List of type Integer or it's super class, you can do:

    public void print(List<? super Integer> list)  // OK
    

    but you can't use type parameter:

     public <T super Integer> void print(List<T> list)  // Won't compile
    

References:

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 4
    This is strange answer. It doesn't explain why you need to use `?` at all. You could rewrite it as `public static void copy(List dest, List src) and in this case it become obvious what is going on. – kan Aug 11 '13 at 21:29
  • @kan. Well that's the real issue. You can use type parameter to enforce same type, but you can't do that with wildcards. Using two different types for type parameter is different thing. – Rohit Jain Aug 11 '13 at 21:31
  • @kan. See my last two points for why you might need to use wildcards. – Rohit Jain Aug 11 '13 at 21:33
  • Rohit what do you mean by type parameter supports upper bounds only? – benz Aug 11 '13 at 21:36
  • 1
    @benz. You can't define a lower bounds in a `List` using type parameter. `List` isn't valid, and won't compile. – Rohit Jain Aug 11 '13 at 21:37
  • This all is making me go back to foundation real difference between wild card and generic methods :)thanks rohit for your explanation – benz Aug 11 '13 at 21:40
  • 2
    @benz. You're welcome :) I highly suggest you to go through the link I posted at the end. That is the best resource on Generics you would get. – Rohit Jain Aug 11 '13 at 21:42
  • Sure rohit I will completely go through it and bug you back with the question. Thank you so much for your precious time. – benz Aug 11 '13 at 21:46
  • "Type parameters support multiple bounds, wildcards don't." "Wildcards support both upper and lower bounds, type parameters just support upper bounds." I didn't quite get this.. What do you mean by "type parameters support multiple bounds"? It just support upper, as you wrote in the next sentence. – jorgen.ringen Jun 29 '16 at 11:57
  • 6
    @jorgen.ringen `` -> multiple bounds. – Rohit Jain Jun 29 '16 at 18:12
  • "If you have only one parameterized type argument, then you can use wildcard, although type parameter will also work." I think that statement is not always true especially when you have a generic method where you want to update, for example, the list you've passed as an argument with items in the list (say, you want to re-add all the items again). – stdout Jul 25 '18 at 12:56
  • 1
    Then why is Java defined so that we can't use the super keyword for type parameter? – user3509406 Nov 05 '18 at 03:12
32

Consider following example from The Java Programming by James Gosling 4th edition below where we want to merge 2 SinglyLinkQueue:

public static <T1, T2 extends T1> void merge(SinglyLinkQueue<T1> d, SinglyLinkQueue<T2> s){
    // merge s element into d
}

public static <T> void merge(SinglyLinkQueue<T> d, SinglyLinkQueue<? extends T> s){
        // merge s element into d
}

Both of the above methods have the same functionality. So which is preferable? Answer is 2nd one. In the author's own words :

"The general rule is to use wildcards when you can because code with wildcards is generally more readable than code with multiple type parameters. When deciding if you need a type variable, ask yourself if that type variable is used to relate two or more parameters, or to relate a parameter type with the return type. If the answer is no, then a wildcard should suffice."

Note: In book only second method is given and type parameter name is S instead of 'T'. First method is not there in the book.

chammu
  • 1,275
  • 1
  • 18
  • 26
12

In your first question: It means that if there is a relation between the parameter's type and the method's return type then use a generic.

For example:

public <T> T giveMeMaximum(Collection<T> items);
public <T> Collection<T> applyFilter(Collection<T> items);

Here you are extracting some of the T following a certain criteria. If T is Long your methods will return Long and Collection<Long>; the actual return type is dependent on the parameter type, thus it is useful, and advised, to use generic types.

When this is not the case you can use wild card types:

public int count(Collection<?> items);
public boolean containsDuplicate(Collection<?> items);

In this two example whatever the type of the items in the collections the return types will be int and boolean.

In your examples:

interface Collection<E> {
    public boolean containsAll(Collection<?> c);
    public boolean addAll(Collection<? extends E> c);
}

those two functions will return a boolean whatever is the types of the items in the collections. In the second case it is limited to instances of a subclass of E.

Second question:

class Collections {
    public static <T> void copy(List<T> dest, List<? extends T> src) {
    ...
}

This first code allow you to pass an heterogeneous List<? extends T> src as a parameter. This list can contain multiple elements of different classes as long as they all extends the base class T.

if you had:

interface Fruit{}

and

class Apple implements Fruit{}
class Pear implements Fruit{}
class Tomato implements Fruit{}

you could do

List<? extends Fruit> basket = new ArrayList<? extends Fruit>();
basket.add(new Apple());
basket.add(new Pear());
basket.add(new Tomato());
List<Fruit> fridge = new ArrayList<Fruit>(); 

Collections.copy(fridge, basket);// works 

On the other hand

class Collections {
    public static <T, S extends T> void copy(List<T> dest, List<S> src) {
    ...
}

constrain List<S> src to be of one particular class S that is a subclass of T. The list can only contain elements of one class (in this instance S) and no other class, even if they implement T too. You wouldn't be able to use my previous example but you could do:

List<Apple> basket = new ArrayList<Apple>();
basket.add(new Apple());
basket.add(new Apple());
basket.add(new Apple());
List<Fruit> fridge = new ArrayList<Fruit>();

Collections.copy(fridge, basket); /* works since the basket is defined as a List of apples and not a list of some fruits. */
diyoda_
  • 5,274
  • 8
  • 57
  • 89
le-doude
  • 3,345
  • 2
  • 25
  • 55
  • 2
    `List extends Fruit> basket = new ArrayList extends Fruit>();` Is not a valid syntax. You have to instantiate the ArrayList without bounds. – Arnold Pistorius Apr 29 '15 at 07:52
  • Cannot add an Apple to the basket in the example above since the basket could be a list of Pears'. Incorrect example AFAIK. And does not compile as well. – Khanna111 Dec 03 '16 at 09:16
  • 1
    @ArnoldPistorius That do confused me. I checked the API documentation of ArrayList and it has a constructor signed `ArrayList(Collection extends E> c)` . Can you explain me why you have said that? – TomasMolina Feb 12 '18 at 21:12
  • @Kurapika Could be that I was using an old Java version? Comment was posted almost 3 years ago. – Arnold Pistorius Feb 22 '18 at 10:43
4

Wildcard method is also generic - you could call it with some range of types.

The <T> syntax defines a type variable name. If a type variable has any use (e.g. in method implementation or as a constraint for other type), then it makes sense to name it, otherwise you could use ?, as anonymous variable. So, looks like just a short-cut.

Moreover, the ? syntax is not avoidable when you declare a field:

class NumberContainer
{
 Set<? extends Number> numbers;
}
kan
  • 28,279
  • 7
  • 71
  • 101
3

I will try and answer your question, one by one.

Don't we think wild card like (Collection<? extends E> c); is also supporting kind of polymorphism?

No. The reason is that the bounded wildcard has no defined parameter type. It is an unknown. All it "knows" is that the "containment" is of a type E (whatever defined). So, it cannot verify and justify whether the value provided matches the bounded type.

So, it's no sensible to have polymorphic behaviours on wildcards.

The document discourages the second declaration and promotes usage of first syntax? What's the difference between the first and second declaration? Both seems to be doing the same thing?

The first option is better in this case as T is always bounded, and source will definitely have values (of unknowns) that subclasses T.

So, suppose that you want to copy all list of numbers, the first option will be

Collections.copy(List<Number> dest, List<? extends Number> src);

src, essentially, can accept List<Double>, List<Float>, etc. as there is an upper bound to the parameterized type found in dest.

The 2nd option will force you to bind S for every type you want to copy, like so

//For double 
Collections.copy(List<Number> dest, List<Double> src); //Double extends Number.

//For int
Collections.copy(List<Number> dest, List<Integer> src); //Integer extends Number.

As S is a parameterized type that needs binding.

I hope this helps.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 1
    Can you explain what do you mean in your last paragraph – benz Aug 11 '13 at 21:43
  • 1
    The one that states 2nd option will force you to bind one......can you elaborate over it – benz Aug 11 '13 at 21:47
  • `` states that `S` is a parameterized type that is a subclass of `T`, so it requires a parameterized type (no wildcard) that is a subclass of `T`. – Buhake Sindi Aug 11 '13 at 21:48
2

One other difference which is not listed here.

static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
    for (T o : a) {
        c.add(o); // correct
    }
}

But the following will result in compile time error.

static <T> void fromArrayToCollection(T[] a, Collection<?> c) {
    for (T o : a) {
        c.add(o); // compile time error
    }
}
Rob
  • 26,989
  • 16
  • 82
  • 98
Vivek Kumar
  • 419
  • 4
  • 12
2

? means unknown

The general rule applies: You can read from it, but not write

given simple pojo Car

class Car {
    void display(){

    }
}

This will compile

private static <T extends Car> void addExtractedAgain1(List<T> cars) {
    T t = cars.get(1);
    t.display();
    cars.add(t);
}

This method won't compile

private static void addExtractedAgain2(List<? extends Car> cars) {
    Car car = cars.get(1);
    car.display();
    cars.add(car); // will not compile
}

Another example

List<?> hi = Arrays.asList("Hi", new Exception(), 0);

hi.forEach(o -> {
   o.toString() // it's ok to call Object methods and methods that don't need the contained type
});

hi.add(...) // nothing can be add here won't compile, we need to tell compiler what the data type is but we do not know
MagGGG
  • 19,198
  • 2
  • 29
  • 30
0

As far as I understand, there is only one use case when wildcard is strictly needed (i.e. can express something that you can not express using explicit type parameters). This is when you need to specify a lower bound.

Apart from that however wildcards serve to write more concise code, as described by the following statements in the document you mention:

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used.

[...]

Using wildcards is clearer and more concise than declaring explicit type parameters, and should therefore be preferred whenever possible.

[...]

Wildcards also have the advantage that they can be used outside of method signatures, as the types of fields, local variables and arrays.

0

Mainly -> Wildcards enforce generics at the parameter/argument level of a Non-Generic method. Note. It can also be performed in genericMethod by default, but here instead of ? we can use T itself.

package generics;

public class DemoWildCard {


    public static void main(String[] args) {
        DemoWildCard obj = new DemoWildCard();

        obj.display(new Person<Integer>());
        obj.display(new Person<String>());

    }

    void display(Person<?> person) {
        //allows person of Integer,String or anything
        //This cannnot be done if we use T, because in that case we have to make this method itself generic
        System.out.println(person);
    }

}

class Person<T>{

}

SO wildcard has its specific usecases like this.

Arasn
  • 138
  • 10