Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回值的菱形运算符_Java_Generics - Fatal编程技术网

Java 返回值的菱形运算符

Java 返回值的菱形运算符,java,generics,Java,Generics,这是否适用于Java7?(我只安装了Java 6。) List<> customers = service.getCustomers(); // returns List<Customer> List customers=service.getCustomers();//返回列表 谢谢。否,菱形用于避免由于声明两次泛型而生成样板代码: List<> customers = service.getCustomers(); // returns List<

这是否适用于
Java7
?(我只安装了Java 6。)

List<> customers = service.getCustomers(); // returns List<Customer>
List customers=service.getCustomers();//返回列表

谢谢。

否,菱形用于避免由于声明两次泛型而生成样板代码:

List<> customers = service.getCustomers(); // returns List<Customer>
Java 6:

List<> customers = service.getCustomers(); // returns List<Customer>
List<List<String>> myList= new ArrayList<List<String>>()
List myList=new ArrayList()
Java 7等效代码:

List<> customers = service.getCustomers(); // returns List<Customer>
 List<List<String>> myList = new ArrayList<>() 
 //the generic is declared once
List myList=new ArrayList()
//泛型声明一次

但在您的示例中,菱形运算符没有“原始泛型”声明来假定正确的类型,因此它不会编译。

否,菱形用于避免由于声明两次泛型而生成的样板代码:

List<> customers = service.getCustomers(); // returns List<Customer>
Java 6:

List<> customers = service.getCustomers(); // returns List<Customer>
List<List<String>> myList= new ArrayList<List<String>>()
List myList=new ArrayList()
Java 7等效代码:

List<> customers = service.getCustomers(); // returns List<Customer>
 List<List<String>> myList = new ArrayList<>() 
 //the generic is declared once
List myList=new ArrayList()
//泛型声明一次

但在您的示例中,菱形运算符没有“原始泛型”声明来假定正确的类型,因此它不会编译。

不,这是编译时错误

List<> customers = service.getCustomers(); // returns List<Customer>
类型列表的参数数量不正确;不可能 参数化的

List<> customers = service.getCustomers(); // returns List<Customer>

不,这是一个编译时错误

List<> customers = service.getCustomers(); // returns List<Customer>
类型列表的参数数量不正确;不可能 参数化的

List<> customers = service.getCustomers(); // returns List<Customer>

将此作为更新Java版本的好理由。将此作为更新Java版本的好理由。