我希望我的问题听起来不会很愚蠢,但我想知道这两行之间有什么区别:
(假设我创建了一个名为 Cow 的类..)
ArrayList<Cow> c1 = new ArrayList<Cow>();
ArrayList<Cow> c2 = new ArrayList();
提前感谢您的解释。
最佳答案
目前,本质上没有区别,因为 type erasure 。优选第一种形式;第二种形式使用原始类型,并且仅支持向后兼容旧版本的 Java。一个好的编译器如果你使用它,会给你警告。
如the Java Language Specification, Third Edition, §4.8解释:
The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.
(强调原文)。
关于Java:ArrayList初始化(两行代码之间的差异),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9501514/