Learnitweb

Preview feature in Java

1. Introduction

A preview feature is a new feature whose design, specification, and implementation are complete, but which is not permanent, which means that the feature may not exist in future JDK release but if exists then may exist in a different form.

Every preview feature is described by JDK Enhancement Proposal (JEP). For example, JEP 325 describes the JDK 12 preview feature for switch expressions. Releasing a feature as a preview in a mainline JDK version allows the widest developer audience to test it in real-world scenarios and offer feedback. Using the preview feature helps in evaluating whether the feature has any long term role in JDK release or not. If any refinement is required, fix can be provided and the feature can be provided as a permanent feature in future JDK release. Based on the feedback, the feature’s preview period may be extended or removed completely.

Here are some examples of issues which can be found during preview period:

  • technical errors, such as type errors.
  • backward compatibility and usability with older feature
  • architectural issues which make the feature extensible and difficult to be used in future

A preview feature allows tool developers to try the feature and provide the support for future releases.

2. Using Preview Features

Preview features are disabled by default. To use preview language features in your programs, you must explicitly enable them in the compiler and the runtime system. If you use a preview feature without enabling the feature, you’ll receive an error message that states that your code is using a preview feature and preview features are disabled by default.

To compile source code with javac that uses preview features from JDK release n, use javac from JDK release n with the --enable-preview command-line option in conjunction with either the --release n or -source n command-line option.

For instance, if you have an application named MyApp.java that uses the JDK 12 preview language feature, switch expressions, you can compile it with JDK 12 using the following command:

javac --enable-preview --release 12 MyApp.java

Note: When you compile an application that uses preview features, you’ll receive a warning message similar to the following:

Note: MyApp.java uses preview language features.
Note: Recompile with -Xlint:preview for details

To run an application that uses preview features from JDK release n, use java from JDK release n with the --enable-preview option. To run MyApp, run java from JDK 12 as follows:

java --enable-preview MyApp

Note:Code that uses preview features from an older release of the Java SE Platform will not necessarily compile or run on a newer release.

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

3. Benefits of Preview Features

  • Early Feedback: Developers can provide feedback on new features, allowing the Java development team to refine and improve them before final release.
  • Real-World Testing: Using preview features in real-world applications helps identify potential issues and ensures the feature works as intended in various scenarios.
  • Informed Decisions: Developers can explore and evaluate new features to see how they might benefit their projects, providing insights into future development plans.

4. Caveats and Considerations

  • Non-Permanent: Preview features are not guaranteed to be included in future Java releases. They may be modified or removed based on feedback and further development.
  • Backward Compatibility: Code using preview features may need to be updated if the feature changes in future releases. Be prepared for potential refactoring.
  • Production Use: It is generally advised to avoid using preview features in production code until they become a permanent part of the language or API.

5. Conclusion

Preview features are a powerful way to get early access to potential new additions to Java, providing an opportunity to experiment and offer feedback. By following the steps outlined above, you can compile and run your code with these features enabled, helping to shape the future of Java development.