Java Preview Features – Java预览功能

最后修改: 2020年 1月 15日

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

1. Overview

1.概述

In this tutorial, we’re going to explore the motivation behind Java preview features, their difference compared to experimental features, and how to enable them with different tools.

在本教程中,我们将探讨Java预览功能背后的动机,它们与实验性功能相比的区别,以及如何用不同的工具启用它们。

2. Why Preview Features

2.为什么预览功能

As it’s probably clear to everyone by now, Java feature releases are delivered every six months. This means less waiting time for new Java features, but at the same time, it means also less time to react to feedback about new features.

现在大家可能都很清楚,Java功能的发布是每六个月一次。这意味着对新的Java功能的等待时间更短,但同时也意味着对新功能的反馈作出反应的时间更短。

This is Java we’re talking about here. It’s used to develop a huge number of production systems. As a result, even a small malfunction in one implementation or a poor feature design could turn out to be very costly.

我们在这里讨论的是Java。它被用来开发大量的生产系统。因此,即使是一个实现中的小故障或一个糟糕的功能设计,都可能变成非常昂贵的代价。

There must be a way to ensure new features are stable. More importantly, they have to suit the needs of the community. But how?

必须有一种方法来确保新功能的稳定性。更重要的是,它们必须适合社区的需要。但是怎么做呢?

Thanks to JEP-12, “review language and VM features” can be included in the deliveries. This way, the community can check out new features in real-life scenarios – surely not in production, however.

由于JEP-12,”审查语言和虚拟机功能 “可以包括在交付的产品中。这样,社区可以在真实的场景中检查新的功能–不过肯定不是在生产中。

Based on community feedback, a preview feature could be refined, possibly several times over multiple releases. Eventually, the feature may become permanent. But in some cases, the provided reviews could lead to withdrawing a preview feature entirely.

基于社区的反馈,一个预览功能可以被改进,可能在多个版本中被多次改进。最终,该功能可能成为永久性的。但在某些情况下,所提供的评论可能会导致完全撤销一个预览功能。

3. Preview Versus Experimental Features

3.预览与实验的特点

Java preview features are completely specified and developed features that are going through evaluation. Therefore, they have just not reached the final state yet.

Java 预视功能是完全指定和开发的功能,正在经历评估。因此,它们只是还没有达到最终状态。

Because of their high quality, different JDK implementations must include all preview features planned within each Java delivery. However, a Java release still can’t support preview features from earlier releases.

由于质量高,不同的 JDK 实现必须包括每个 Java 交付中计划的所有预览功能。但是,一个Java版本仍然不能支持早期版本的预览功能

Preview features are essentially just a way to encourage the community to review and provide feedback. Moreover, not every Java feature must go through a preview stage in order to become final.

预览功能本质上只是一种鼓励社区审查和提供反馈的方式。此外,并不是每个Java功能都必须经过预览阶段才能成为最终功能。

Here’s what JEP-12 has to say about preview features:

下面是JEP-12对预览功能的评价。

A preview language or VM feature is a new feature whose design, specification, and implementation are all complete, but which would benefit from a period of broad exposure and evaluation before either achieving final and permanent status in the Java SE Platform or else being refined or removed.

预览语言或虚拟机特性是一种新特性,其设计、规范和实现都已完成,但在获得Java SE平台的最终和永久地位之前,它将从一段广泛的曝光和评估中受益,否则将被完善或删除。

On the other hand, experimental features are far from complete. Their artifacts are clearly separated from the JDK artifacts.

另一方面,实验性功能远未完成。它们的工件与JDK的工件明显分开。

Experimental features are unstable and, as such, they impose a risk upon the language. Consequently, different JDK implementations may include different sets of experimental features.

实验性功能是不稳定的,因此,它们会给语言带来风险。因此,不同的JDK实现可能包括不同的实验性功能集。

4. Using Preview Features

4.使用预览功能

Preview features are disabled by default. To enable them, we must use the enable-preview argument, which enables all preview features at once.

预览功能默认是禁用的。要启用它们,我们必须使用enable-preview参数,它可以一次性启用所有预览功能。

The Java compiler, as well as the JVM, must be of the same Java version that includes the preview feature we want to use.

Java编译器以及JVM必须是包括我们要使用的预览功能的同一Java版本

Let’s try to compile and run a piece of code that uses text blocks, a preview feature within JDK 13:

让我们试着编译和运行一段使用文本块的代码,这是JDK 13中的一个预览功能。

String query = """
    SELECT 'Hello World'
    FROM DUAL;
    """;
System.out.println(query);

Of course, we need to make sure we’re using JDK 13 with our favorite IDE. We can, for instance, download the OpenJDK release 13 and add it to our IDE’s Java runtime.

当然,我们需要确保我们在我们最喜欢的IDE中使用JDK 13。例如,我们可以下载 OpenJDK release 13,并将其添加到我们 IDE 的 Java 运行时中。

4.1. With Eclipse

4.1.使用Eclipse

At first, Eclipse will mark the code with red, as it won’t compile. The error message will tell us to enable preview features in order to use text blocks.

一开始,Eclipse会将代码标记为红色,因为它无法编译。错误信息会告诉我们要启用预览功能,以便使用文本块。

We need to right-click on the project and select Properties from the pop-up menu. Next, we go to Java Compiler. Now, we can choose to enable preview features either for this specific project or for the entire workspace.

我们需要右击项目,从弹出菜单中选择属性。接下来,我们转到Java Compiler。现在,我们可以选择为这个特定的项目或为整个工作区启用预览功能。

Next, we have to uncheck Use default compliance settings, and only then can we check Enable preview features for Java 13:

接下来,我们必须取消勾选使用默认合规设置,然后才能勾选为Java 13启用预览功能

4.2. With IntelliJ IDEA

4.2.使用IntelliJ IDEA

As we’d expect, the code won’t compile in IntelliJ by default either, even with Java 13, and we’ll get an error message similar to the one we saw in Eclipse.

正如我们所期望的那样,代码在IntelliJ中也不会默认编译,即使是用Java 13,我们也会得到一个类似于我们在Eclipse中看到的错误信息。

We can enable preview features from Project Structure in the File menu. From Project, we need to select 13 (Preview) as the Project language level:

我们可以从File菜单中的Project Structure启用预览功能。从项目,我们需要选择13(预览)作为项目语言级别。

This should do it. However, if the error still persists, we have to manually add the compiler arguments to enable preview features. Assuming it’s a Maven project, the compiler plugin in the pom.xml should contain:

这应该可以了。然而,如果错误仍然存在,我们必须手动添加编译器参数以启用预览功能。假设这是一个Maven项目,pom.xml中的编译器插件应该包含。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>13</source>
                <target>13</target>
                <compilerArgs>
                    --enable-preview
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

If required, we can enable preview features for other Maven plugins within their respective configurations in a similar way.

如果需要,我们可以在其他Maven插件各自的配置中以类似方式启用预览功能。

4.3. From Command Line

4.3.从命令行

At compile time, the javac command needs two arguments — enable-preview and release:

在编译时,javac命令需要两个参数–enable-previewrelease

javac --release 13 --enable-preview ClassUsingTextBlocks.java

Let’s recall that a JDK release N doesn’t support preview features of release N-1 or any previous releases. Therefore, we’ll get an error if we try to execute the previous command with JDK 14.

让我们回顾一下,JDK版本N不支持N-1版本或任何以前版本的预览功能。因此,如果我们试图用JDK 14执行前面的命令,我们会得到一个错误。

Long story short, the release argument must set N to the JDK release version of the compiler (and JVM) being used in order to enable preview features.

长话短说,release参数必须将N设置为正在使用的编译器(和JVM)的JDK发布版本,以便启用预览功能。

The release argument is just an extra guard to ensure code using preview features won’t be eagerly used in production.

release参数只是一个额外的防护措施,以确保使用预览功能的代码不会被急于在生产中使用。

At runtime, the java command only requires the enable-preview argument:

在运行时,java命令只需要enable-preview参数。

java --enable-preview ClassUsingTextBlocks

However, only code using the preview features of that specific JDK release would run.

然而,只有使用该特定JDK版本的预览功能的代码才能运行。

5. Conclusion

5.总结

In this article, we’ve introduced preview features in Java, why we have them, and how they differ from experimental features.

在这篇文章中,我们介绍了Java中的预览功能,为什么有这些功能,以及它们与实验性功能有什么不同。

Then, using the text blocks preview feature in JDK 13, we explained step by step how to use preview features from Eclipse, IntelliJ, Maven, and the command line.

然后,利用JDK 13中的文本块预览功能,我们一步步解释了如何使用Eclipse、IntelliJ、Maven和命令行的预览功能。