Java 7中的钻石操作符有什么意义? [英] What is the point of the diamond operator in Java 7?

查看:1077
本文介绍了Java 7中的钻石操作符有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java 7中的钻石运算符允许如下代码:

  List< String> list = new LinkedList<>(); 

然而,在Java 5/6中,我可以简单地写出:

 列表< String> list = new LinkedList(); 

我对类型擦除的理解是,它们完全相同。 (无论如何,这个泛型在运行时会被删除)。

为什么要钻石呢?它允许什么新的功能/类型安全?如果它没有产生任何新的功能,为什么他们提到它作为一个功能?

 列表与LT;字符串> list = new LinkedList(); 

是在左边,您使用的是通用类型列表< String> 在右侧使用 raw 类型 LinkedList 。 Java中的原始类型实际上只存在与前泛型代码的兼容性,并且不应该在新代码中使用,除非
您绝对必须使用。



现在,如果Java从一开始就有泛型,并没有类型,比如 LinkedList ,它们最初是在泛型之前创建的,它可能会使得构造函数如果可能,泛型类型会自动从赋值的左侧推断其类型参数。但它没有,并且为了向后兼容,它必须以不同的方式处理原始类型和通用类型。这就让他们需要做一个稍微不同的,但同样方便的方式来声明一个通用对象的新实例,而不必重复它的类型参数......钻石运算符。



就您最初的 List< String> list = new LinkedList(),编译器会为该分配生成一个警告,因为它必须。考虑一下:

  List< String> strings = ... //包含某些字符串的某些列表

//完全合法,因为您使用了原始类型并丢失了所有类型的检查!
列表<整数>整数=新的LinkedList(字符串);

泛型提供编译时保护,防止错误操作。在上面的例子中,使用原始类型意味着你没有得到这种保护,并会在运行时得到一个错误。这就是为什么你不应该使用原始类型。

  //不合法,因为右侧实际上是通用的! 
列表<整数>整数=新的LinkedList<>(字符串);

但是,菱形运算符允许将赋值的右侧定义为真正的泛型实例具有与左侧相同的类型参数...无需再次输入这些参数。它使您可以像使用原始类型一样保持泛型的安全。



我认为要理解的关键是原始类型(不包含<> )不能被视为与泛型类型相同。当你声明一个原始类型时,你不会得到泛型的好处和类型检查。您还必须记住,泛型是Java语言的通用部分 ...它们不仅适用于 Collection 的无参数构造函数, code> s!


The diamond operator in java 7 allows code like the following:

List<String> list = new LinkedList<>();

However in Java 5/6, I can simply write:

List<String> list = new LinkedList();

My understanding of type erasure is that these are exactly the same. (The generic gets removed at runtime anyway).

Why bother with the diamond at all? What new functionality / type safety does it allow? If it doesn't yield any new functionality why do they mention it as a feature? Is my understanding of this concept flawed?

解决方案

The issue with

List<String> list = new LinkedList();

is that on the left hand side, you are using the generic type List<String> where on the right side you are using the raw type LinkedList. Raw types in Java effectively only exist for compatibility with pre-generics code and should never be used in new code unless you absolutely have to.

Now, if Java had generics from the beginning and didn't have types, such as LinkedList, that were originally created before it had generics, it probably could have made it so that the constructor for a generic type automatically infers its type parameters from the left-hand side of the assignment if possible. But it didn't, and it must treat raw types and generic types differently for backwards compatibility. That leaves them needing to make a slightly different, but equally convenient, way of declaring a new instance of a generic object without having to repeat its type parameters... the diamond operator.

As far as your original example of List<String> list = new LinkedList(), the compiler generates a warning for that assignment because it must. Consider this:

List<String> strings = ... // some list that contains some strings

// Totally legal since you used the raw type and lost all type checking!
List<Integer> integers = new LinkedList(strings);

Generics exist to provide compile-time protection against doing the wrong thing. In the above example, using the raw type means you don't get this protection and will get an error at runtime. This is why you should not use raw types.

// Not legal since the right side is actually generic!
List<Integer> integers = new LinkedList<>(strings);

The diamond operator, however, allows the right hand side of the assignment to be defined as a true generic instance with the same type parameters as the left side... without having to type those parameters again. It allows you to keep the safety of generics with almost the same effort as using the raw type.

I think the key thing to understand is that raw types (with no <>) cannot be treated the same as generic types. When you declare a raw type, you get none of the benefits and type checking of generics. You also have to keep in mind that generics are a general purpose part of the Java language... they don't just apply to the no-arg constructors of Collections!

这篇关于Java 7中的钻石操作符有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆