What is the difference between using generic type and a wildcard in Java Generics?

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. 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.

Example:

Suppose you want to ensure that the src and dest list passed to copy() method should be of the 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 a 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 a wildcard:

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

It won’t work as expected. In the 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 differences between using wildcards and type parameters are:

  • If you have only one parameterized type argument, then you can use a 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 superclass, 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

source: stackoverflow