简体   繁体   English

新的LinkedList <>()与新的LinkedList()有何不同

[英]How does new LinkedList<>() differ from new LinkedList()

I just stumbled upon the compiler treating these two terms differently. 我只是偶然发现编译器对这两个术语的处理方式不同。 when I type: 当我输入:

LinkedList<String> list = new LinkedList();

I get a compiler warning about a raw type. 我收到有关原始类型的编译器警告。 however: 然而:

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

removes the warning. 删除警告。 It seems to me as though the two statements mean essentially the same thing (ie create a new LinkedList with no specified object type). 在我看来,好像这两个语句基本上是相同的东西(即创建一个没有指定对象类型的新LinkedList)。 Why then does the complier all ow the empty generics? 为什么编译器都有空的泛型呢? What is the difference here? 这有什么区别?

It's the diamond operator in Java 7, that helps you save writing the type again. 它是Java 7中的菱形运算符,可以帮助您再次保存写入类型。 In Java 7 this is equivalent to the same generic type argument that is used on the left side of the declaration. 在Java 7中,这相当于声明左侧使用的相同泛型类型参数。 So the initialization is type safe and no warning is issued. 因此初始化是类型安全的,不会发出警告。

The statements do not mean the same thing at all. 这些陈述并不意味着相同的事情。

The first statement tries to fit an untyped LinkedList into a declared generic LinkedList<String> and appropriately throws a warning. 第一个语句尝试将无类型的LinkedList拟合到声明的通用LinkedList<String>并适当地抛出警告。

The second statement, valid in Java 1.7 onward, uses type inference to guess the type parameter by using the declaring type's type parameter. 第二个语句在Java 1.7以后有效,它使用类型推断通过使用声明类型的类型参数来猜测类型参数。 In addition, sometimes this can be used in method calls. 此外,有时这可以在方法调用中使用。 It doesn't always work, however. 然而,它并不总是有效。

See this page for more info. 有关详细信息,请参阅此页面

With LinkedList<>, you use the new Diamond Operator, from java 7. 使用LinkedList <>,您可以使用java 7中的新Diamond Operator。

The Diamod operator uses the generic value setted in the left side of the line. Diamod运算符使用在行左侧设置的通用值。

In Java 6, this doesnt works! 在Java 6中,这不起作用!

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语言的通用部分......它们不仅仅适用于集合的无参数构造函数!

Extracted from: https://stackoverflow.com/a/10093701/1281306 摘自: https//stackoverflow.com/a/10093701/1281306

Backword compatibility (Inter-operating with legacy code) is the reason why java allows above signature. 后缀兼容性(与遗留代码互操作)是java允许上述签名的原因。 Generics are compile time syntax only. 泛型只是编译时语法。 At runtime "all generic" syntax will be removed. 在运行时,将删除“所有通用”语法。 You will just see if you de-compile any class file. 您将看到是否解编译任何类文件。 Read this documentation . 阅读此文档

LinkedList list = new LinkedList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM