29

Can somebody explain me what the difference is between these two methods? Are they same? They do look same to me in terms of what they solve. If they are same, why need ??

Method #1, Unbounded

public static void printList(List<?> list) {
    for (Object elem : list)
        System.out.println(elem + " ");
    System.out.println();
}

Method #2, Unbounded:

public static <T> void printList(List<T> list) {
    for (Object elem : list)
        System.out.println(elem + " ");
    System.out.println();
}

Method #1, Bounded

public static void printList(List<? extends Number> list) {
    for (Object elem : list)
        System.out.println(elem + " ");
    System.out.println();
}

Method #2, Bounded:

public static <T extends Number> void printList(List<T> list) {
    for (Object elem : list)
        System.out.println(elem + " ");
    System.out.println();
}
Tarik
  • 79,711
  • 83
  • 236
  • 349

3 Answers3

36

They are the same in that they accept the same parameter types.

However, identifying the type with T (or whatever) lets you refer to the type elsewhere.

Edit: Examples:

Your unbounded examples do not make full use of the capabilities of parameterized types. You have:

public static <T> void printList(List<T> list) {
    for (Object elem : list)
        System.out.println(elem + " ");
    System.out.println();
}

And that's sufficient for that example of printing string representations, but consider this (very contrived, and no error handling):

public static <T> T getSecondItem (List<T> list) {
    T item = list.get(1);
    return item;
}

The return type is T, which allows you to safely do things like this, with compile time type-checking:

class MyClass {
    public void myMethod () { }
}

void somewhere () {
    List<MyClass> items = ...;
    getSecondItem(items).myMethod();
}

A named type also lets you share the same type constraint in multiple places, e.g.:

public <T> int compareLists (List<T> a, List<T> b) {
    ...
}

If you did not name the type, you could not specify the constraint that a and b are the same list type (you could use List<? extends T> for more flexibility).

You also asked "Why do I need ??". The real answer is: You don't. It's mostly for aesthetics, I suppose. Java strives to be a precise and clutter-free language. There are many situations where you simply don't care what type you are referring to. In those cases, you may use ? without cluttering code with unused type parameter declarations.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 2
    Then why would I need to use `?`? – Tarik Aug 14 '13 at 20:56
  • @Tarik When you don't need to refer to the type elsewhere inside the method. You're not forced to though. Both will compile fine. – Sotirios Delimanolis Aug 14 '13 at 20:59
  • 2
    By using a wildcard, you're explicitely saying: I don't care about the concrete type of the elements of the list, and what I do doesn't depend on this type. If the method had to return a T, or pass the T to some other generic type, then you would use a typed method. – JB Nizet Aug 14 '13 at 21:00
  • OK, but is there anything we could not achieve without `?`? I think this was also included in the original question. – Katona Aug 14 '13 at 21:08
  • 3
    @Katona Not really. But it allows you to keep your code free of unused type parameters, e.g. `void op (List extends Base> a, List extends Base> b)` instead of `void op (List a, List b)`. – Jason C Aug 14 '13 at 21:12
  • 5
    @Katona To echo what Jason is saying, wildcard captures are essentially type parameters that are never referenced again. But they are not always just shortcuts. Note that they might be used elsewhere besides method parameters. Wildcards can be used anywhere and aren't confined to a scope like type parameters - for example `static final List> MY_LIST;` could not use a type parameter instead. There are also *nested wildcards* which have [entirely different semantics](http://stackoverflow.com/questions/3546745). A method `void m(List> l)` is different from ` void m(List> l)`. – Paul Bellora Aug 15 '13 at 04:53
  • 1
    Re: "is there anything we could not achieve without `>`", actually there is code we can write with `>` that we can't write (in a type safe way) without it. See the example with `List>` in this answer: http://stackoverflow.com/a/10777906 Without `>` you'd have to use which would not be type safe. – okonomichiyaki Mar 30 '14 at 14:30
6

In your examples, there is absolutely no difference at all. Each will produce the same output.

The best use and interpretation of generics requires that you know the semantics of the type parameter as well as something about the parameter's role. The reason for this is that in a case such as your first example above (unbounded wild card) the semantics are "a list of objects of unknown type" and a "parameter that will produce (not consume) List<?> instances.

The method above simply produces each List<?> object and calls toString() on each. All objects are guaranteed to have a toString() method and so it isn't necessary to know anything at all about the object's type for this purpose. This is exactly why the unbounded wildcard is the best choice for this method parameter: To produce List<?> instances and to call toString() on them, it is not necessary to know anything about the object's type.

Note that if the ? had the same semantics ("a List of objects of unknown type") but a different purpose ("the List would consume objects of unknown type") things would change very, very radically such that it may be inadvisable (or very difficult) to use a wildcard parameter (at least without a helper method to capture the object's type).

It is generally not possible to assert one generic parameter form for all situations. The best form to use (wildcard vs. concrete; bounded vs. unbounded; extends vs. super) depends on both the semantics of the type parameter and what the role of the parameter in the method will be.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
scottb
  • 9,908
  • 3
  • 40
  • 56
5

As mentioned above, the ? states that the generic code does not require any reference to a type. As pointed in the Java Trails, this could be a class where the properties are independent of any type like the length of a collection.

Another reason for the ? is that it provides syntax for less ambiguity for the case when the generic code only needs behavior provided by class Object. As pointed out above, the parameter type <T> would work, but the fact that T was defined and then never used could suggest that the developer left out something.

So if <T> is dropped because of semantic ambiguity and <?> is not available, then the developer would be tempted to write <Object>. This however does not allow the generic code to be used for any type, just the Object type.

So, <?> is more precise.

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
Henry Nelson
  • 51
  • 1
  • 1
  • This is a great point. The `>` very clearly self-documents that the type is intentionally not used elsewhere. – Jason C Feb 24 '14 at 21:09