Java 简明教程
Java 10 - New Features (APIs & Options)
Java 10 是 Java 发布节奏中的一个主要版本,它于 2018 年 3 月 10 日发布。对于 Java 10,Oracle 已将 java 发布节奏更改为一种新模式,为 Oracle Java SE 产品制定了 6 个月的发布节奏和 LTS 模式。LTS 模式代表长期支持模式。
Java 10 is a major release in Java release cadence and it was releasd on March 10, 2018. With Java 10, Oracle has changed the java release cadence to a new model, a 6 month release cadence and LTS model for Oracle Java SE products. LTS model stands for Long Term Support model.
从 Java 10 开始,Oracle 每 6 个月发布一个新版本的 Java,每个版本包含一个或两个主要特性。Oracle 使用发布周期概念。每个发布周期安排 6 个月。在这个时间范围内开发的特性都会在该版本中发布,否则该特性将移至下一个发布周期。
From Java 10 onwards, Oracle releases a new version of Java after every 6 month where each version contains one or two major features. Oracle uses a release train concept. Each release train is scheduled for 6 months. Features which are developed within this timeline are shipped in the release otherwise the features are moved to next release train.
Oracle JDK vs OpenJDK
大多数 Oracle JDK 二进制文件是专有的,由 Oracle 许可,并且对再分发有多种限制。而 OpenJDK 更适合开发人员社区。从 Java 10 开始,Oracle 决定将 OpenJDK 作为主要的 JDK 来促进基于社区的 Java 开发。Oracle 将继续生成自己的 JDK,但将在 3 年后发布它们,并将其称为 LTS 版本。因此,OpenJDK 二进制文件将在每六个月后发布。
Most of the Oracle JDK binaries are propriety and licensed by Oracle and have multiple restrictions on redistribution. Whereas OpenJDK is more developer community friendly. From Java 10 onwards, Oracle has decided to promote OpenJDK as primary JDK to facility community based development of Java. Oracle will keep producing its own JDKs but it will release them after 3 years and term them as LTS version. So OpenJDK binaries will be released after every six month.
OpenJDK 兼容云和容器,因为它可以作为容器的一部分免费分发。因此,Oracle 推广 OpenJDK 的举措使 Java 更适合于云或容器的开发和部署。
OpenJDK is cloud and container friendly as it can freely distributed as part of the container. So Oracle’s move to promote OpenJDK makes java more friendly towards cloud or container development and deployment.
Java 9 和 Java 10 是非 LTS 版本。Java 11 版本是 LTS 版本。
Java 9 and Java 10 are non-LTS release. Java 11 release is a LTS release.
Java 10 New Features
以下是在 Java 10 中引入的主要新特性。
Following are the major new features which are introduced in Java 10.
-
JEP 286 − Local Variable Type Inference
-
JEP 322 − Time-Based Release Versioning
-
JEP 304 − Garbage-Collector Interface
-
JEP 307 − Parallel Full GC for G1
-
JEP 296 − Consolidate the JDK Forest into a Single Repository
-
JEP 310 − Application Class-Data Sharing
-
JEP 319 − Root Certificates
-
JEP 317 − Experimental Java-Based JIT Compiler
-
JEP 312 − Thread-Local Handshakes
-
JEP 313 − Remove the Native-Header Generation Tool
在 Java 10 中,对 70 多个 API 进行增强,添加了新方法和选项,并移除了已弃用的 API 和选项。我们将在下一章中了解这些更改。
Java 10 enhanced 70+ APIs with new methods and options and removed deprecated APIs and options. We’ll see these changes in next chapters.
Java 10 - New APIs & Options
JDK 10 发布已为 Java 库添加了 70 多个新 API 和选项。以下是一些引入的重要增强功能。
JDK 10 release has added 70+ new APIs and Options to Java library. Following are some of the important enhancements introduced.
Optional.orElseThrow() Method
一个新方法 orElseThrow() 在 java.util.Optional 类中提供,它现在是 get() 方法的首选替代方法。
A new method orElseThrow() is available in java.util.Optional class which is now a preferred alternative for get() method.
APIs to create Unmodifiable Collections
在 List、Set 和 Map 接口中有新的 copyOf() 方法,它可从现有集合中创建新的集合实例。Collector 类拥有方法 toUnmodifiableList(), toUnmodifiableSet(), and toUnmodifiableMap(),用于将流的元素弄成不可修改的集合。
A new method copyOf() is available in List, Set and Map interfaces which can create new collection instances from existing one. Collector class has new methods toUnmodifiableList(), toUnmodifiableSet(), and toUnmodifiableMap() to get elements of a stream into an unmodifiable collection.
Disable JRE Last Usage Tracking
引入了新标志 jdk.disableLastUsageTracking,它可禁用针对正在运行的 VM 的 JRE 上次使用情况跟踪。
A new flag is introduced jdk.disableLastUsageTracking which disables JRE last usage tracking for a running VM.
Hashed Password
JMX 代理现在已使用 SHA3-512 哈希值覆盖 jmxremote.password 文件中可用的明文密码。
The plain text passwords available in the jmxremote.password file are now being over-written with their SHA3-512 hash by the JMX agent.
javadoc Support for Multiple Stylesheets
新的选项以 --add-stylesheet 的形式提供给 javadoc 命令。此选项支持在生成的文档中使用多个样式表。
A new option is available to javadoc command as --add-stylesheet. This option supports use of multiple stylesheets in generated documentation.
javadoc Support for Overridding methods
新的选项以 --overridden-methods=value 的形式提供给 javadoc 命令。由于许多类均会覆盖继承的方法但不会更改规范,因此 --overridden-methods=value 选项允许将这些方法与其他继承方法组合到一起,而不是单独再次描述它们。
A new option is available to javadoc command as --overridden-methods=value. As many classes override inherited methods but do not change the specification. The --overridden-methods=value option allows to group these methods with other inherited methods, instead of documenting them again separately.
javadoc Support for Summary
新的内联标签 {@summary …} 提供给指定要作为 API 描述摘要的文本。默认情况下,API 描述的摘要是从第一句话推断出来的。
A new inline tag, {@summary …}, is available to specify the text to be used as the summary of the API description. By default, the summary of an API description is inferred from the first sentence.
Example
以下程序展示了 JAVA 10 中一些新 API 的使用。
Following Program shows the use of some of the new APIs in JAVA 10.
import java.util.List;
import java.util.stream.Collectors;
public class Tester {
public static void main(String[] args) {
var ids = List.of(1, 2, 3, 4, 5);
try {
// get an unmodifiable list
List<Integer> copyOfIds = List.copyOf(ids);
copyOfIds.add(6);
} catch(UnsupportedOperationException e){
System.out.println("Collection is not modifiable.");
}
try{
// get an unmodifiable list
List<Integer> evenNumbers = ids.stream()
.filter(i -> i % 2 == 0)
.collect(Collectors.toUnmodifiableList());;
evenNumbers.add(6);
}catch(UnsupportedOperationException e){
System.out.println("Collection is not modifiable.");
}
}
}