为什么使用类型参数调用非参数化方法不会出错?

Why is it not an error to call a non-parameterized method with type arguments?

我有以下 Java 程序,我原以为它不会编译,但它编译了:

class Test {
    public static void f() {
    }

    void m() {
            Test.<String>f();
    }
}

为什么javac允许以这种方式调用非参数化方法?

我的Java编译器版本是:javac 1.7.0_75

直接忽略显式类型参数。

这在JLS, Section 15.12.2.1中有说明:

  • If the method invocation includes explicit type arguments, and the member is a generic method, then the number of type arguments is equal to the number of type parameters of the method.

This clause implies that a non-generic method may be potentially applicable to an invocation that supplies explicit type arguments. Indeed, it may turn out to be applicable. In such a case, the type arguments will simply be ignored.