【发布时间】:2017-11-21 12:52:50
【问题描述】:
这个问题不是关于为什么我们将列表初始化为接口而不是实现,例如
List<myObject> obj = new ArrayList<myObject>();
问题是以下两者之间有什么区别以及为什么它们(显然)以相同的方式工作?
//list and arraylist both have a type
List<myObject> obj = new ArrayList<myObject>();
//arraylist does not have a type
List<myObject> obj = new ArrayList<>();
【问题讨论】:
-
它们是同一个东西。菱形运算符是在 java 7 中作为快捷方式引入的,这样您作为程序员就不必不必要地重复自己:docs.oracle.com/javase/7/docs/technotes/guides/language/…