Week 150 — What is a preview JDK feature?

Question of the Week #150
What is a preview JDK feature?
8 Replies
MrMisha | Coder
MrMisha | Coder2mo ago
Once JDK features are fully released, a lot of people will rely on them so they can't be changed without breaking these people's code. To allow giving feedback before something is fully set in stone, a feature can be released as a preview feature. These are fully finished and specified language, API or VM features for wider testing which are still subject to change. Because preview features are still subject to change with new (major) Java versions, these are not backwards compatible and have to be explicitly enabled. To enable the use of preview features, developers can pass --enable-preview to javac. This will result in javac accepting code using preview features but it also results in the compiled class file being valid if and only if the --enable-preview flag is passed when running java and the same (major) Java version must be used for compiling and running the application.
MrMisha | Coder
MrMisha | Coder2mo ago
In contrast to preview features, incubator modules are not (necessarily) completely specified but are still planned for being included in the JDK and benefit from wider testing. Incubator modules can be used solely for API features (and tools) but not for language or VM features. The name of incubator modules starts with jdk.incubator to make them distinct from other parts of the JDK.
📖 Sample answer from dan1st
MrMisha | Coder
MrMisha | Coder2mo ago
A preview JDK feature is an experimental Java feature released for developers to test and provide feedback before it’s finalized, and it must be explicitly enabled since it can change or be removed in future versions.
Submission from gaucheyy
MrMisha | Coder
MrMisha | Coder2mo ago
A jdk preview is a new feature which may or may not be permanent, developers are encouraged to build and support the feature before using it in production. The feedback from developers determines whether the feature has any design mistake which includes hard technical error and soft usability problems or poor architectural choices, through this feedback the feature's strength and weakness are evaluated to determine if it has lts role in Java SE Platform. If yes then it is granted to be final or permanent status. Example to enable preview: For Specific version: compile MyApp: javac --enable-preview --release 12 MyApp.java run MyApp java --enable-preview MyApp To Find version wise preview: explore release notes of the desired version.
Submission from aizen_souske123
MrMisha | Coder
MrMisha | Coder2mo ago
Preview JDK feature is a JVM feature that is fully specified but not yet permanent. It is released in a JDK so that developers can try it out and give feedback, but it may change or even be removed in future releases. Preview features are not enabled by default, and we must explicitly enable them when compilling and running the code. Code Example: public class PreviewFeature { public static void main(String[] args) { Object obj1 = "Hello World!"; String result = switch(obj1) { case String s -> "It is a string" + s; case Integer i -> "It is an integer" + i; default -> "Unknown Type"; }; System.out.println(result); } }
Submission from prsoumyajit
MrMisha | Coder
MrMisha | Coder2mo ago
A preview feature is a "try-before-you-buy" feature in Java — available for testing and feedback, but not yet permanent.
Submission from nafisatou123
MrMisha | Coder
MrMisha | Coder2mo ago
When the Java community wants to introduce new features to the language, JVM, or JDK, they will often label the enhancement as a "preview". This means that the feature is considered fully-specified, but some real-world usage and community feedback would help to solidify the design or implementation. Preview features help to support the compatibility guarantees that the Java platform makes -- the features that make it into the platform are of higher quality, and are therefore more useful and easier to support. Preview features must be explicitly enabled using the --enable-preview flag on both javac (at compilation time) and on java (at runtime), e.g.:
javac --enable-preview SomeProgram.java
java --enable-preview SomeProgram
javac --enable-preview SomeProgram.java
java --enable-preview SomeProgram
Build tools like Gradle & Maven also allow this flag to be configured:
tasks.withType<JavaCompile>().configureEach {
options.compilerArgs.add("--enable-preview")
}

tasks.withType<Test>().configureEach {
jvmArgs("--enable-preview")
}

tasks.withType<JavaExec>().configureEach {
jvmArgs("--enable-preview")
}
tasks.withType<JavaCompile>().configureEach {
options.compilerArgs.add("--enable-preview")
}

tasks.withType<Test>().configureEach {
jvmArgs("--enable-preview")
}

tasks.withType<JavaExec>().configureEach {
jvmArgs("--enable-preview")
}
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>x.y.z</version>
<configuration>
<release>XX</release>
<enablePreview>true</enablePreview>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>x.y.z</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>--enable-preview</argument>
<argument>SomeProgram</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>x.y.z</version>
<configuration>
<release>XX</release>
<enablePreview>true</enablePreview>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>x.y.z</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>--enable-preview</argument>
<argument>SomeProgram</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
The preview feature as it existed for a specific Java version can be enabled by passing the --release XX option to javac, along with the --enable-preview flag. This might be useful, for instance, if you have code that was created against the preview features of that specific Java version, but now needs to be compiled under a newer JDK. Since that newer JDK might support the final feature, or a refinement of the preview, the older code might not work as-is. Forcing the compiler to use the compatible preview version would be required. That said, the code should be brought into line with the final or most recent preview of the feature as soon as possible, in order to ensure long-term compatibility.
MrMisha | Coder
MrMisha | Coder2mo ago
Note that Java also supports "incubating" features, which are similar to "preview" features, except that incubating features are not considered "feature-complete". They are much more likely to change than previews, possibly even in significant ways. They may also remain in incubation for many versions (e.g. Vector API, which is currently on its 11th incubation cycle), whereas previews usually go through 1 or just a few cycles before being finalized.
⭐ Submission from dangerously_casual

Did you find this page helpful?