Java 语言更新(Java 8-Java 11 QFY译版)



Java SE 11 中 Java 语言更改

Java SE 11 允许用 var 标识符声明隐式类型的lambda表达式的形式参数;参见局部变量类型推断。 == Java SE 10 中的 Java 语言更改

JavaSE 10引入了从上下文推断局部变量类型的支持,这使得代码更可读,减少了所需样板代码的数量。


在Java SE 10以后,可以用 var 标识符来声明非空初始化器的局部变量,这可以帮助您编写更易于阅读的代码。看下面的示范代码,看起来比较繁琐,不方便阅读:

URL url = new URL("");
URLConnection conn = url.openConnection();
Reader reader = new BufferedReader(
    new InputStreamReader(conn.getInputStream()));

可以使用 var 标识符声明局部变量重写这个示例。变量的类型是从上下文推断的:

var url = new URL("");
var conn = url.openConnection();
var reader = new BufferedReader(
    new InputStreamReader(conn.getInputStream()));

var 是保留的类型名,而不是关键字,这意味着使用 var 作为变量、方法或包名的现有代码不受影响。但是,使用var作为类或接口名称的代码受到影响,需要重命名类或接口。var 可用于以下类型的变量:

  • 带有初始化器的局部变量声明:

    var list = new ArrayList<String>();    // infers ArrayList<String>
    var stream =;            // infers Stream<String>
    var path = Paths.get(fileName);        // infers Path
    var bytes = Files.readAllBytes(path);  // infers bytes[]
  • 增强的循环索引:

    List<String> myList = Arrays.asList("a", "b", "c");
    for (var element : myList) {...}  // infers String
  • 在传统 for 循环中声明的索引变量:

    for (var counter = 0; counter < 10; counter++)  {...}   // infers int
  • try-with-resources 变量:

    try (var input =
         new FileInputStream("validation.txt")) {...}   // infers FileInputStream
  • 隐式类型lambda表达式的形式参数声明:隐式类型化形式参数具有推断类型的lambda表达式:

    BiFunction<Integer, Integer, Integer> = (a, b) -> a + b;

    自 Java SE 11 起,您可以用 var 标识符声明隐式类型的lambda表达式的每个形式参数:

    (var a, var b) -> a + b;

    因此,隐式类型的lambda表达式中的形式参数声明的语法与局部变量声明的语法是一致的。NOTE: 注意隐式类型lambda表达式中 使用 `var `与否具有相同的效果。


    (var x, y) -> x.process(y)      // 不能混用 var 和类型推断参数
                                    // 在隐式类型的lambda表达式中
    (var x, int y) -> x.process(y)  // 不能混用 var 和声明参数类型
                                    // 显式类型 lambda 表达式

Java SE 9 中 Java语言更改

JavaSE 9 的主要改变是引入Java平台模块系统。Java平台模块系统引入了一种新的Java编程组件模块,模块是一个命名的、自描述的代码和数据集合。它的代码被组织成一组包含类型的包,即Java类和接口;它的数据包括资源和其他类型的静态信息。模块可以导出或封装包,并且它们明确地表达了对其他模块的依赖。要了解有关Java平台模块系统的更多信息,请参见OpenJDK上的项目 Jigsaw 。除了新的模块系统之外,Java语言也有一些改变。本指南的其余部分描述了这些改变。

try-with-resources 更简洁

如果资源变量是 final 或 effectively final ,则可以在 try-with-resources 语句中使用该变量,而无需声明新变量。一个 effectively final 变量是一个值,在初始化后其值永远不变。例如,您声明了这两个资源:

        // A final resource
        final Resource resource1 = new Resource("resource1");
        // An effectively final resource
        Resource resource2 = new Resource("resource2");

在Java SE 7或8中,您需要声明新的变量,如下所示:

        try (Resource r1 = resource1;
             Resource r2 = resource2) {

在Java SE 9中,不需要声明R1和R2:

// New and improved try-with-resources statement in Java SE 9
        try (resource1;
             resource2) {

在Java教程(Java SE 8和更早版本)中有 try-with-resources 语句的更完整描述。 === 小语言更改

Java SE 9中有几个小的语言变化。

允许接口私有方法使用 @SafeVarargs 注解。

@SafeVarargs 注释只能应用于无法重写的方法。

这些方法包括静态方法、最终实例方法、以及Java SE 9 中新加的私有实例方法。


可以在Java程序中写入的类型,如 int 或 String,称为可指示类型。不能在Java程序中编写的编译器内部类型称为不可指明类型。不可指明类型可由菱形运算符推断出来。因为使用匿名类构造函数的菱形推断的类型可以在类文件中属性类型集之外,所以在JavaSE 7中不允许使用匿名类的菱形推断。在Java SE 9中,只要推断类型是可指示的,就可以在创建匿名内部类时使用菱形运算符。




支持私有接口方法。这样允许接口的非抽象方法在它们之间共享代码。 == 文档网址及版权

For information about Oracle’s commitment to accessibility, visit the Oracle Accessibility Program website at Access to Oracle Support Oracle customers that have purchased support have access to electronic support through My Oracle Support. For information, visit or visit if you are hearing impaired. Java Platform, Standard Edition Java Language Updates, Release 11 E94884-01 Copyright © 2017, 2018, Oracle and/or its affiliates. All rights reserved. Describes the updated language features in Java SE 9 and subsequent releases. This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, then the following notice is applicable: U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to the programs. No other rights are granted to the U.S. Government. This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group. This software or hardware and documentation may provide access to or information about content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services unless otherwise set forth in an applicable agreement between you and Oracle. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services, except as set forth in an applicable agreement between you and Oracle.