You are currently viewing Going beyond Java 8: Feature Preview

Going beyond Java 8: Feature Preview

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 one 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-months 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 new a 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 use a feature preview, and the -source option to specify the Java version for which we want to enable it (the options are highlighted in bold). For example:

javac --enable-preview -source 15 RecordDemo.java

Instead of the -source option, the -release option can be equivalently specified:

javac --enable-preview -release 15 RecordDemo.java

To run an application that uses a preview feature, you only need to enable the preview feature like this:

java --enable-preview RecordDemo

The javadoc and jshell tools also support the --enable-preview option.

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.

 

Example

The 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 switch expression:

import java.time.Month;

public class SeasonSwitchExpressionEnumTest {
    public static void main(String args[]) {
        Month month = Month.APRIL;
        String season = switch(month) {
            case DECEMBER, JANUARY, FEBRUARY: yield "winter";
            case MARCH, APRIL, MAY: yield "spring";
            case JUNE, JULY, AUGUST: yield "summer";
            case SEPTEMBER, OCTOBER, NOVEMBER: yield "autumn";
        };
        System.out.println("The season is " + season);
    }
}

we will get the following compile-time errors:

javac SeasonSwitchExpressionEnumTest.java
SeasonSwitchExpressionEnumTest.java:9: error: switch expressions are a preview feature and are disabled by default.
        String season = switch(month) {
                        ^
  (use --enable-preview to enable switch expressions)
SeasonSwitchExpressionEnumTest.java:10: error: multiple case labels are a preview feature and are disabled by default.
            case DECEMBER, JANUARY, FEBRUARY -> "winter";
                         ^
  (use --enable-preview to enable multiple case labels)
SeasonSwitchExpressionEnumTest.java:10: error: switch rules are a preview feature and are disabled by default.
            case DECEMBER, JANUARY, FEBRUARY -> "winter";
                                             ^
  (use --enable-preview to enable switch rules)
3 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 13 to specify the Java version for which we want to enable it:

javac --enable-preview -source 13 SeasonSwitchExpressionEnumTest.java

Instead of the -source option, the -release option can be specified equivalently:

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

Instead, to launch the example, you only need to enable the preview functionalities:

java --enable-preview SeasonSwitchExpressionEnumTest

 

Why is it important?

In version 12 of Java, when switch expressions were introduced as feature preview, the keyword break was used instead of the word yield. Only in version 13 yield replaced 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 record types.

 

How to send feedback to Oracle

Feedback on feature preview, 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 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)

Figure 1 – Understanding the email address to send feedback to.

Unfortunately, this is not a link, but it is a string to be interpreted as amber-dev@openjdk.java.net. 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 want just to report a bug, you can click directly on the link of the Java Bug Database.

 

Conclusion

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.

 

Author’s Notes

This article is based on a few paragraphs from my English book “Java for Aliens“.

 

Leave a Reply