Uses of T extends U?

Lin*_*ica 17 java generics

Lately I encountered a method defined similar to this and I don't exactly understand its usage:

public static <T, U extends T> T foo(U u) { ... }
Run Code Online (Sandbox Code Playgroud)

A sample use could be like this:

// Baz is just the containing class of foo()
Number n = Baz.foo(1);
Run Code Online (Sandbox Code Playgroud)

Where T is inferred to Number and U (probably) to Integer. But I can't wrap my head around when this is superior to e.g. this method definition:

public static <T> T bar(T t) { ... }
Run Code Online (Sandbox Code Playgroud)

If I call it like this:

Number n = Baz.bar(2);
Run Code Online (Sandbox Code Playgroud)

The code still works. T is inferred to either Number or Integer (Don't know if the argument type, in this example Integer is preferred over the call site return type Number)

I've read these questions: 1, 2 but I still don't know if the first method with 2 parameters has any advantage over the second method with only one generic.

Mar*_*o13 9

I think that, in fact, this only makes sense when the type parameter of the method appears as the type parameter of a parameterized type that is part of the method signature.

(At least, I couldn't quickly come up with an example where it would really makes sense otherwise)

This is also the case in the question that you linked to, where the method type parameters are used as type parameters in the AutoBean class.


A small update:

Based on the discussion in the question and other answers, the core of this question was likely a misinterpretation of the way of how the type parameters are used. As such, this question could be considered as a duplicate of Meaning of <T, U extends T> in java function declaration , but hopefully someone will consider this answer helpful nevertheless.

In the end, the reason for using the pattern of <T, U extends T> can be seen in the inheritance relationships of parameterized types, which in detail may be fairly complicated. As an example, to illustrate the most relevant point: A List<Integer> is not a subtype of a List<Number>.


An example showing where it can make a difference is below. It contains a "trivial" implementation that always works (and does not make sense, as far as I can tell). But the type bound becomes relevant when the type parameters T and U are also the type parameters of the method parameters and return type. Whith the T extends U, you can return a type that has a supertype as the type parameter. Otherwise, you couldn't, as shown with the example that // Does not work:

import java.util.ArrayList;
import java.util.List;

public class SupertypeMethod {
    public static void main(String[] args) {

        Integer integer = null;
        Number number = null;

        List<Number> numberList = null;
        List<Integer> integerList = null;

        // Always works:
        integer = fooTrivial(integer);
        number = fooTrivial(number);
        number = fooTrivial(integer);

        numberList = withList(numberList);
        //numberList = withList(integerList); // Does not work

        // Both work:
        numberList = withListAndBound(numberList);
        numberList = withListAndBound(integerList);
    }

    public static <T, U extends T> T fooTrivial(U u) {
        return u;
    }

    public static <T, U extends T> List<T> withListAndBound(List<U> u) {
        List<T> result = new ArrayList<T>();
        result.add(u.get(0));
        return result;
    }

    public static <T> List<T> withList(List<T> u) {
        List<T> result = new ArrayList<T>();
        result.add(u.get(0));
        return result;
    }

}
Run Code Online (Sandbox Code Playgroud)

(This looks a bit contrived, of course, but I think that one could imagine scenarios where this actually makes sense)

  • @Lino的确,不是这样的“ U扩展T”,而是经常(在本示例中)可以使用`?扩展签名中的T`。这是不可能的情况,例如,“ List &lt;T&gt; copyAndAdd(List &lt;U&gt; us,U anotherU)”,其中第二个参数必须是“ U”。 (5认同)