Going Beyond Java 8: Feature Preview
Learn about enabling the feature preview process in order to test new Java features, why it's important, and how to send feedback to Oracle.
Join the DZone community and get the full member experience.Join For Free
According to some surveys, such as that of JetBrains, version 8 of Java is currently the most used by developers all over the world, despite being a 2014 release.
What you are reading is the first in a series of articles titled 'Going beyond Java 8,' inspired by the contents of my book 'Java for Aliens.' These articles will guide the reader step by step to explore the most important features introduced, starting from version 9. The aim is to make the reader aware of how important it is to move forward from Java 8, explaining the enormous advantages that the latest versions of the language offer.
Oracle is making several improvements to Java programming by accelerating the language development process with the six-month release model. From version 12, with
switch expressions and text blocks, a new type of approach has been introduced for the launch of new features, using the so-called feature preview process. In practice, a new feature is introduced in order to be usable as a preview for experimental purposes. In this way, developers can test it, give feedback to Oracle, which in turn can improve it in future versions.
Enabling Feature Preview
In order to use a new feature preview, you must specify some command-line options at both compile and runtime.
In particular, you need to specify the
--enable-preview option when compiling to enable code that uses a feature preview and the
-source option to specify the Java version for which we want to enable it (highlighted in bold). For example:
Instead of the
-source option, the
-release option can be equivalently specified:
To run an application that uses a preview feature, you only need to enable the preview feature like this:
The javadoc and jshell tools also support the
As for the various development tools that we usually use for coding (Eclipse, Netbeans, IntelliJ, Maven, etc.), being subject to continuous updates and bug fixing, we will have to read to the corresponding documentation. Usually, this involves changing the project settings, specifying the compile and runtime options we have described.
switch expression has been a feature preview in Java versions 12 and 13 before becoming officially a standard feature in version 14. In fact, if we wanted to compile with a JDK version 13 the following class that uses a
we will get the following compile-time errors:
In order to correctly compile this class containing the switch expression, you need to specify on the command-line options that we introduced in the previous section. In particular
--enable-preview to enable the feature preview, and
-source 13to specify the Java version for which we want to enable it:
Instead of the
-releaseoption can be specified equivalently:
Instead, to launch the example, you only need to enable the preview functionalities:
Why Is It Important?
In version 12 of Java, when
switch expressions were introduced as a feature preview, the keyword
break was used instead of the word
yield. Only in version 13
break. In fact, most of the feedback for this feature preview judged the use of the
break keyword misleading, as it is already used in other contexts. This demonstrates the importance of the feature preview process. In practice, Java developers were able to participate even more actively in the development process of the new features. With the feature preview process, Java is even more likely to be a language increasingly appreciated by developers, as they will be able to express their opinions before the feature is made official.
Furthermore, in addition to the
switch expressions we mentioned in the previous example, other fundamental features have been introduced with this preview-based path. These include, for example, pattern matching for
instanceof, text blocks,
sealed types and even
How to Send Feedback to Oracle
Feedback on feature previews can be sent to Oracle by posting in the discussions on the OpenJDK mailing list. Unfortunately, this is not a very intuitive process. In particular, to find the discussion about a particular feature preview, you must go to the page that lists the new features of the version of the JDK you are using, click on the link of the feature preview on which to return the feedback, and interpret the string titled discussion as an email address. Then you can then send your feedback to this email address.
For example, if you are using version 15 and want to send feedback on
sealed types feature preview, you need to go to https://openjdk.java.net/projects/jdk/15 (to change the version, just change the number '15' to the desired version number). Find the Sealed Classes (Preview) link in the list and click on it. On the page that opens, under the title, you will find a list of information, including a discussion item, which in this case is followed by the string 'amber dash dev at openjdk dot java dot net' (see Figure 1).
Unfortunately, this is not a link, but it is a string to be interpreted as. In practice, you can send an email containing the feedback to this address. In particular, to subscribe to the project mailing list, you can go to this address: http://mail.openjdk.java.net/mailman/listinfo/amber-dev.
If you just want to report a bug, you can click directly on the link of the Java Bug Database.
In this short article, we have seen how to enable the feature preview and to be able to use the features that will be introduced in the next versions of Java, even contributing with feedback if that's the case. We also understood why previews are important and how the process of introducing new features is more democratic now since anyone can contribute to the standardization of the language. This will help Java to become an increasingly interesting language.
Published at DZone with permission of Claudio De Sio Cesari. See the original article here.
Opinions expressed by DZone contributors are their own.