在Java规范(http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9)中,new具有以下形式:
ClassInstanceCreationExpression ::=
| new TypeArguments_opt TypeDeclSpecifier TypeArgumentsOrDiamond_opt
( ArgumentListopt ) ClassBodyopt
| Primary . new TypeArguments_opt Identifier TypeArgumentsOrDiamond_opt
( ArgumentListopt ) ClassBodyopt
Run Code Online (Sandbox Code Playgroud)
新的后面的第一个可选类型参数列表的目的是什么?我无法从我对15.9节的阅读中找到它(对类型参数列表的所有引用似乎都引用了类型/标识符之后的列表).测试标准Java编译器上的随机位会产生令人困惑的结果:
public class Foo<T> { }
// ...
Foo<Integer> t1 = new <Integer> Foo<Integer>(); // works
Foo<Integer> t2 = new <Integer> Foo(); // works -- unchecked warning missing the type arg after Foo
Foo<Integer> t3 = new <Boolean> Foo<Integer>(); // works
Foo<Integer> t4 = new <Float, Boolean> Foo<Integer>(); // works
Foo<Integer> t5 = new <NotDefined> …Run Code Online (Sandbox Code Playgroud) java ×1