最近,Oracle采用了一种新策略,即每六个月发布一次新的语言版本。 该策略假定只有每个第3版都将提供长期支持或LTS。 快速说明:
- 具有LTS的当前版本是Java 8;
- Java 9仅应在2018年3月之前获得支持,因此已经结束。
- Java 10支持到2018年9月;
- 具有LTS的下一个版本应该是Java 11,该版本应该在2018年9月发布,并且至少将支持到2023年9月。
Oracle Java SE支持路线图中的更多信息。
我们公司仍然没有采用Java 9,但是现在看来完全跳过它并跳到下一个LTS可能是一个有效的选择。 除了Java本身,还有很多其他问题,例如Spring Boot Java版本等,因此我们可能会谨慎行事。 尽管如此,鉴于不可避免的更改,我还是决定研究Java 10中的功能。该版本中的主要功能似乎是本地类型推断 。
我们都知道这种Java语法:
List<User> list = new ArrayList<User>();
// or since Java 7
List<User> list = new ArrayList<>();
基本上,局部类型推断可以替换为:
// left side type is inferred from the right side and will be ArrayList
var userList = new ArrayList();
这意味着代码的样板要少一些,但是您需要更多地注意变量和方法名,因为var关键字不是不言自明的。
许多编程语言(例如Scala,C#,Go,当然还有Kotlin)中的本地类型推断已经存在了很长时间。 在这方面,Java一直处于落后地位,现在已决定对其进行修复。 但是,关于此的具体实现存在一些争议。 例如,存在以下可能性:
- 如Kotlin或Scala中那样, val用于局部常量,而var用于变量;
- 因为const和final已经在Java中保留了,所以对于局部常量使用const或仅final作为变量,对变量使用var ;
- 具有用于对变量常数和变种 最后变种 ;
- 利用let , def或:= ;
- 还有更多关于此的内容。
最终,决定使语法更接近现有语法,并允许var用于局部变量, final var用于局部常量,因此,上面显示的示例将在Java 10中运行。 但是,替换并非总是那么简单。 例如:
// example 1 - list is a List<User> type
List<User> list = new ArrayList<>();// example 2 - userList is an ArrayList<Object> type, so you lose type information
var userList = new ArrayList<>();// example 3 - userListFixed is an ArrayList<User> type, so you keep type information
var userListFixed = new ArrayList<User>();
在示例二中,在直接替换左侧的情况下,编译器无法推断列表类型,因此它将默认为Object。 当您尝试处理列表中的项目时,它将使您绊倒。
局部类型推断很有用的一个例子是数据转换。 例如,您想将一种类型的对象更改为另一种具有不同属性的对象,并为此使用一个匿名类。 在Java 8中,您只能在流范围内进行操作,例如,可以在流管道中访问新属性,但不能在外部访问。
List<User> users = Arrays.asList(new User("Elisabeth", "Bennett", 20),new User("Jane", "Bennett", 22),new User("Mary", "Bennett", 18),new User("Kitty", "Bennett", 17),new User("Lydia", "Bennett", 15));users.stream().map(u ->new Object() {String fullName = u.firstName + " " + u.lastName;boolean canDrink = u.age >= 18;}).forEach(u -> {if (u.canDrink) {System.out.println("+ " + u.fullName + " is of age and can drink");} else {System.out.println("- " + u.fullName + " is not of age and cannot drink");}});/* Output will be* + Elisabeth Bennett is of age and can drink* + Jane Bennett is of age and can drink* + Mary Bennett is of age and can drink* - Kitty Bennett is not of age and cannot drink* - Lydia Bennett is not of age and cannot drink*/
在Java 10中,可以在流管道外部使用新对象的转换列表。
List<User> users = Arrays.asList(new User("Elisabeth", "Bennett", 20),new User("Jane", "Bennett", 22),new User("Mary", "Bennett", 18),new User("Kitty", "Bennett", 17),new User("Lydia", "Bennett", 15));final var objects = users.stream().map(u ->new Object() {String fullName = u.firstName + " " + u.lastName;boolean canDrink = u.age >= 18;}).collect(Collectors.toUnmodifiableList());// do something with the users...System.out.println();for (var o : objects) {if (o.canDrink) {System.out.println("+ " + o.fullName + " is of age and can drink");} else {System.out.println("- " + o.fullName + " is not of age and cannot drink");}}
因此,流关闭结束后,仍然可以访问映射的对象属性。 但是,由于这是局部类型推断,因此您将无法在当前方法的局部范围之外使用它们。 Var关键字在方法参数声明中不起作用,因此您不能将var传递给另一个方法。 因此,拥有var并不意味着Java神奇地变成了动态类型。 它仍然是静态类型的,只是添加了一点语法糖,并且仅在编译器可以推断类型的地方使用。
对我而言,这表明Java一直在努力前进,但由于试图保持其历史根源,使向后兼容而不是创新成为其主要优先事项而受到极大阻碍。
翻译自: https://www.javacodegeeks.com/2018/04/local-type-inference-in-java-10-or-if-it-quacks-like-a-duck.html