Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Language Features by Version

Java preview features

How Java preview and incubator features work: the --enable-preview flag, --release, class-file marking, what changes between previews and how to check your JDK.

A preview feature is a complete, specified and implemented part of the language, the JVM or the standard library that is released for feedback but is not yet permanent. Its design may change in the next release, or it may be withdrawn (string templates were previewed in Java 21 and 22 and then removed). Preview features are off by default: you must opt in on every compile and every run.

The idea is to let developers try a feature on real code for one or more six-month releases before it is frozen forever. Once a feature is final it stays compatible for decades, which is why the OpenJDK team prefers to iterate in the open first.

Terminal
# Read-only: the runner on this page does not pass --enable-preview
javac --release 25 --enable-preview Demo.java
java --enable-preview Demo

# Source launcher: one step
java --enable-preview --source 25 Demo.java

Kinds of experimental features

Kind What it is How you enable it
Preview language feature New syntax or semantics, such as primitive patterns --enable-preview (plus --release N or --source N on the compiler)
Preview API A normal-looking class that is flagged by the compiler, such as StructuredTaskScope or StableValue in Java 25 --enable-preview
Incubator module An API that lives in a jdk.incubator.* module, such as the Vector API --add-modules jdk.incubator.vector
Experimental JVM option A JVM feature behind -XX:+UnlockExperimentalVMOptions the -XX flags

Try it

The sandbox runs normal (non-preview) Java, so the first example shows what this means in practice, and the second shows how to ask the JVM what it has.

Java Which Java is this, and which classes exist?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java The shape of the opt-in rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

How previews are enforced

  • javac --enable-preview needs --release N (or --source N) with N equal to the JDK you run. You cannot target an older release with preview features.
  • A class file that uses preview language features or preview APIs is stamped with minor version 65535. The JVM refuses to load such a class unless it runs with --enable-preview on the same feature release. Recompile after every JDK upgrade.
  • Preview APIs are annotated inside the JDK. Calling one without the flag gives a compile error such as "is a preview API and is disabled by default".
  • Code compiled from the source launcher gets the same treatment: pass --enable-preview --source N to java File.java.
  • Libraries should not publish preview-dependent artifacts: they would work only for users running exactly that JDK with the flag.
  • Maven and Gradle pass the flag through compiler arguments (<compilerArgs> / options.compilerArgs) and through the test runner's JVM arguments.

What was previewed recently

Final by Java 25: virtual threads (21), record patterns and switch patterns (21), unnamed variables (22), stream gatherers (24), scoped values, module imports, compact source files and flexible constructor bodies (25). Still preview or incubating in Java 25: structured concurrency, primitive types in patterns, StableValue, PEM encodings, and the Vector API (incubator). Check java --list-modules | grep incubator and the release notes of the JDK you actually use.

Usage notes

  • Do not use preview features in production code you cannot recompile. A preview can change or disappear between releases.
  • Previews can appear a second, third, even tenth time with changes; read the new JEP before upgrading.
  • IDEs have a "language level X (Preview)" setting that sets the flag for you.
  • Learning projects and prototypes are the right place to try previews.