静态函数调用前的泛型尖括号 [英] Generics angle brackets before static function call
问题描述
我一直使用泛型,并且总是看到如下所用的尖括号: Class< Type>
(例如 List< String>
)。
I have always used generics and always seen the angle brackets used like this: Class<Type>
(e.g. List<String>
).
今天我在调用静态方法之前遇到了泛型规范,如: Class。< TypeA,TypeB> staticCall()
。真正的例子是: ImmutableMap。< String,String> builder()
。
Today I encountered a generics specification before the call of a static method like: Class.<TypeA, TypeB>staticCall()
. The real example is: ImmutableMap.<String, String>builder()
.
我从未见过这种用法,我在文档中找不到这种特定的用法一>。有人可以解释一下发生了什么吗?
I've never seen this usage and I can't find this specific usage in the documentation. Can someone explain what is going on, please?
推荐答案
这些被称为通用方法。
在Java 7之前,你必须指定通用引用的类型:
Before Java 7 you had to specify the type of the Generic reference:
Util.<Integer, String>compare(p1, p2);
现在编译器从上下文中推断出类型。
Now the compiler infers the type from the context.
这篇关于静态函数调用前的泛型尖括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!