New Features in Java 10 – Java 10的新功能

最后修改: 2018年 5月 26日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

JDK 10, which is an implementation of Java SE 10, was released on March 20, 2018.

JDK 10,它是Java SE 10的实现,于2018年3月20日发布。

In this article, we’ll cover and explore the new features and changes introduced in JDK 10.

在这篇文章中,我们将介绍并探讨JDK 10中引入的新功能和变化。

2. Local Variable Type Inference

2.局部变量的类型推断

Follow the link for an in-depth article on this feature:

请点击链接,了解有关这一功能的深入文章。

Java 10 Local Variable Type Inference

Java 10局部变量类型推断

3. Unmodifiable Collections

3.不可修改的集合

There are a couple of changes related to unmodifiable collections in Java 10.

在Java 10中,有几处与不可修改的集合有关的变化。

3.1. copyOf()

3.1. copyOf()

java.util.List, java.util.Map and java.util.Set each got a new static method copyOf(Collection).

java.util.Listjava.util.Mapjava.util.Set都有一个新的静态方法copyOf(Collection)

It returns the unmodifiable copy of the given Collection:

它返回给定Collection的不可修改的副本:

@Test(expected = UnsupportedOperationException.class)
public void whenModifyCopyOfList_thenThrowsException() {
    List<Integer> copyList = List.copyOf(someIntList);
    copyList.add(4);
}

Any attempt to modify such a collection would result in java.lang.UnsupportedOperationExceptionruntime exception.

任何修改这种集合的尝试都会导致java.lang.UnsupportedOperationExceptionruntime异常。

3.2. toUnmodifiable*()

3.2 ToUnmodifiable*()

java.util.stream.Collectors get additional methods to collect a Stream into unmodifiable List, Map or Set:

java.util.stream.Collectors获得额外的方法,将Stream收集成不可修改的ListMapSet

@Test(expected = UnsupportedOperationException.class)
public void whenModifyToUnmodifiableList_thenThrowsException() {
    List<Integer> evenList = someIntList.stream()
      .filter(i -> i % 2 == 0)
      .collect(Collectors.toUnmodifiableList());
    evenList.add(4);
}

Any attempt to modify such a collection would result in java.lang.UnsupportedOperationExceptionruntime exception.

任何修改这种集合的尝试都会导致java.lang.UnsupportedOperationExceptionruntime异常。

4. Optional*.orElseThrow()

4.Optional*.orElseThrow()

java.util.Optional, java.util.OptionalDouble, java.util.OptionalIntand java.util.OptionalLongeach got a new method orElseThrow()which doesn’t take any argument and throws NoSuchElementExceptionif no value is present:

java.util.Optionaljava.util.OptionalDoublejava.util.OptionalIntjava.util.OptionalLong都有一个新方法orElseThrow,该方法不需要任何参数,如果没有值出现则抛出NoSuchElementException

@Test
public void whenListContainsInteger_OrElseThrowReturnsInteger() {
    Integer firstEven = someIntList.stream()
      .filter(i -> i % 2 == 0)
      .findFirst()
      .orElseThrow();
    is(firstEven).equals(Integer.valueOf(2));
}

It’s synonymous with and is now the preferred alternative to the existing get()method.

它与现有的get()方法同义,现在是首选的替代方法。

5. Performance Improvements

5.性能改进

Follow the link for an in-depth article on this feature:

请点击链接,了解有关这一功能的深入文章。

Java 10 Performance Improvements

Java 10的性能改进

6. Container Awareness

6.集装箱意识

JVMs are now aware of being run in a Docker container and will extract container-specific configuration instead of querying the operating system itself – it applies to data like the number of CPUs and total memory that have been allocated to the container.

JVM现在意识到在Docker容器中运行,并将提取容器特定的配置,而不是查询操作系统本身–它适用于已分配给容器的CPU数量和总内存等数据。

However, this support is only available for Linux-based platforms. This new support is enabled by default and can be disabled in the command line with the JVM option:

然而,这种支持只适用于基于Linux的平台。这个新的支持是默认启用的,可以在命令行中用JVM选项禁用。

-XX:-UseContainerSupport

Also, this change adds a JVM option that provides the ability to specify the number of CPUs that the JVM will use:

另外,这一变化增加了一个JVM选项,提供了指定JVM将使用的CPU数量的能力。

-XX:ActiveProcessorCount=count

Also, three new JVM options have been added to allow Docker container users to gain more fine-grained control over the amount of system memory that will be used for the Java Heap:

此外,还增加了三个新的JVM选项,允许Docker容器用户对将用于Java Heap的系统内存量进行更精细的控制。

-XX:InitialRAMPercentage
-XX:MaxRAMPercentage
-XX:MinRAMPercentage

7. Root Certificates

7.根基证书

The cacerts keystore, which was initially empty so far, is intended to contain a set of root certificates that can be used to establish trust in the certificate chains used by various security protocols.

cacerts密钥库,到目前为止最初是空的,目的是包含一组根证书,可用于建立对各种安全协议所使用的证书链的信任。

As a result, critical security components such as TLS didn’t work by default under OpenJDK builds.

因此,关键的安全组件,如TLS,在OpenJDK构建中默认是不工作的。

With Java 10, Oracle has open-sourced the root certificates in Oracle’s Java SE Root CA program in order to make OpenJDK builds more attractive to developers and to reduce the differences between those builds and Oracle JDK builds.

随着Java 10的推出,Oracle已经将Oracle的Java SE根CA计划中的根证书开源,以使OpenJDK构建对开发者更具吸引力,并减少这些构建与Oracle JDK构建之间的差异。

8. Deprecations and Removals

8.废止和删除

8.1. Command Line Options and Tools

8.1.命令行选项和工具

Tool javah has been removed from Java 10 which generated C headers and source files which were required to implement native methods – now, javac -h can be used instead.

工具javah已从Java 10中删除,该工具生成了实现本地方法所需的C头文件和源文件,现在可以使用javac -h代替。

policytool was the UI based tool for policy file creation and management. This has now been removed. The user can use simple text editor for performing this operation.

policytool是基于用户界面的工具,用于创建和管理策略文件。现在这已被删除。用户可以使用简单的文本编辑器来执行这一操作。

Removed java -Xprofoption. The option was used to profile the running program and send profiling data to standard output. The user should now use jmap tool instead.

删除了java -Xprof选项。该选项用于对运行中的程序进行剖析并将剖析数据发送到标准输出。用户现在应该使用jmap工具代替。

8.2. APIs

8.2.APIs[/strong]

Deprecated java.security.acl package has been marked forRemoval=true and is subject to removal in a future version of Java SE. It’s been replaced by java.security.Policy and related classes.

已废弃的java.security.acl包已被标记为forRemoval=true,并将在未来的Java SE版本中被移除。它已被java.security.Policy和相关的类所取代。

Similarly, java.security.{Certificate,Identity,IdentityScope,Signer} APIs are marked forRemoval=true.

同样,java.security.{Certificate,Identity,IdentityScope,Signer}也被标记为APIs被标记为forRemoval=true

9. Time-Based Release Versioning

9.基于时间的发布版本管理

Starting with Java 10, Oracle has moved to the time-based release of Java. This has following implications:

从Java 10开始,甲骨文已经转向基于时间的Java发布。这有以下影响。

  1. A new Java release every six months. The March 2018 release is JDK 10, the September 2018 release is JDK 11, and so forth. These are called feature releases and are expected to contain at least one or two significant features
  2. Support for the feature release will last only for six months, i.e., until next feature release
  3. Long-term support release will be marked as LTS. Support for such release will be for three years
  4. Java 11 will be an LTS release

java -version will now contain the GA date, making it easier to identify how old the release is:

java -version 现在将包含GA日期,使其更容易识别版本的年代。

$ java -version
openjdk version "10" 2018-03-20
OpenJDK Runtime Environment 18.3 (build 10+46)
OpenJDK 64-Bit Server VM 18.3 (build 10+46, mixed mode)

10. Conclusion

10.结论

In this article, we saw the new features and changes brought in by Java 10.

在这篇文章中,我们看到了Java 10带来的新功能和变化。

As usual, code snippets can be found over on GitHub.

像往常一样,代码片段可以在GitHub上找到over