为什么这种通用方法不会产生编译时错误?

时间:2017-09-19 18:15:47

标签: java generics generic-method

在这个程序中我创建了一个泛型方法,其中第二个参数扩展了第一个参数但是当我将String作为第一个参数传递而Integer数组作为第二个参数时,程序运行正常。为什么不给出编译时错误,因为Integer没有扩展String?

class GenericMethodDemo {

    static <T, V extends T> boolean isIn(T x, V[] y) {

        for (int i = 0; i < y.length; i++) {
            if (x.equals(y[i])) {
                return true;
            }
        }

        return false;

    }

    public static void main(String args[]) {

        Integer nums[] = {1, 2, 3, 4, 5};

        if (!isIn("2", nums)) {
            System.out.println("2 is not in nums");
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这种编译没有错误,因为类型系统将推断两个类型参数之间最近的公共超类型。

在提供的示例中,最近的公共超类型为Object

如果我们提供double作为第一个参数Number将是推断类型,因为它是DoubleInteger之间最近的常见超类型。

public static void main(String args[]) {

    Integer nums[] = {1, 2, 3, 4, 5};

    //In this case the nearest common type is object
    if (!isIn("2", nums)) {
        System.out.println("2 is not in nums");
    }

    //In this case the nearest common type would be Number
    if (!isIn(2d, nums)) {
        System.out.println("2 is not in nums");
    }        
}

正如azurefrog所说,为了防止编译类型证人(GenericMethodDemo.<String, Integer>isIn("2", nums))需要防止类型推断使用最近的公共超类型。

关于类型推断的Java语言规范详细信息可以在这里找到:https://docs.oracle.com/javase/specs/jls/se8/html/jls-18.html