What did Java 25 bring over Java 21 for a working Java developer?
answer
- many previews finally landed
- custom intermediate stream operations
- ThreadLocal's immutable, bounded cousin
- synchronized stops holding the carrier
- a training run for faster startup
basics
~20 sOver Java 21, Java 25 has final scoped values, stream gatherers, the Foreign Function & Memory API, unnamed variables, flexible constructor bodies, module imports and compact source files. Virtual threads no longer pin in synchronized, and ahead-of-time caches speed startup.
solid answer
~40 sBetween Java 21 and Java 25 a long list of previews became final. In the language: `_` for unnamed variables, statements before `super(...)`, `import module`, and compact source files with instance `main`. In libraries: scoped values, stream gatherers, the Foreign Function & Memory API and the Class-File API. At runtime the change I feel most came in Java 24: a virtual thread blocking in `synchronized` no longer pins its carrier. Ahead-of-time caches improve startup and warmup from a training run, and compact object headers are a product option, though off by default at 25. On upgrade, expect warnings for JNI and `sun.misc.Unsafe` memory access, and the Security Manager can no longer be enabled. Structured concurrency is still a preview.
code
java · 16 linesstatic final ScopedValue<String> REQUEST_ID = ScopedValue.newInstance();
void main() {
List<List<Integer>> batches = Stream.of(1, 2, 3, 4, 5)
.gather(Gatherers.windowFixed(2))
.toList();
IO.println(batches);
ScopedValue.where(REQUEST_ID, "r-42").run(() -> IO.println(REQUEST_ID.get()));
try {
Integer.parseInt("not a number");
} catch (NumberFormatException _) {
IO.println("rejected");
}
}go deeper
Recall a few features that are final at 25, such as unnamed variables, compact source files and scoped values, and be able to say that structured concurrency is still a preview.
Explain what each feature does and which release finalized it: statements before super, gatherers, scoped values against ThreadLocal, and what the synchronized change means for a virtual thread.
Show upgrade judgment: new warnings for JNI and Unsafe point at libraries you must chase, the Security Manager cannot be enabled, and an ahead-of-time cache needs a representative training run in your build.
Weigh moving from one long-term release to the next: which warnings signal future failures, whether to standardize scoped values now, and how far to invest in cache training given the startup your deployments need.
## What this question is really about Java 25, released in September 2025, follows Java 22, 23 and 24. Much of what it brings is not new in spirit: features that previewed for several releases became final. A good answer says what you would now use without `--enable-preview`, and what is still not final. ## The language | Feature | Previewed in | Final in | JEP | What it gives you | |---|---|---|---|---| | Unnamed variables and patterns | 21 | 22 | 456 | `_` where a variable or nested pattern is required but never used | | Markdown documentation comments | none | 23 | 467 | `///` comments written in Markdown | | Flexible constructor bodies | 22, 23, 24 | 25 | 513 | Statements before `super(...)`, for validation or assigning this class's fields that have no initializer | | Module import declarations | 23, 24 | 25 | 511 | `import module java.base;` imports every package a module exports | | Compact source files and instance main methods | 21 to 24 | 25 | 512 | A small program with no class declaration and a plain `void main()` | Code before `super(...)` still cannot read the object under construction, and javac rejects it if it tries. It can assign this class's own fields that have no initializer, so a superclass constructor that calls an overridden method no longer sees them unset. Compact source files are not a separate dialect: each one implicitly imports `java.base`, and `java.lang.IO` gives simple console methods such as `IO.println`. ## The libraries - **Foreign Function & Memory API** (JEP 454, final in Java 22, after incubating at 17 and 18 and previews at 19 to 21) calls native code and manages off-heap memory without JNI. It has its own question. - **Stream gatherers** (JEP 485, final in Java 24) add custom intermediate operations through `Stream.gather`, with built-ins such as `Gatherers.windowFixed`, `fold` and `mapConcurrent`. - **Class-File API** (JEP 484, final in Java 24) parses and generates class files. It has its own question too. - **Scoped values** (JEP 506, final in Java 25, after incubating at 20 and previews at 21 to 24) share immutable data with callees and child threads for a bounded scope. They are not a forced replacement: JEP 506 does not deprecate `ThreadLocal`. - **Cryptography** gains the Key Derivation Function API (JEP 510, Java 25), and the quantum-resistant ML-KEM and ML-DSA algorithms (JEPs 496 and 497, Java 24). ## The runtime - **Synchronized without pinning** (JEP 491, Java 24). A virtual thread that blocks in `synchronized` now releases its carrier. Calls into native code can still pin. This is a strong reason to prefer 25 over 21 for virtual threads. - **Ahead-of-time cache** (JEP 483, Java 24). One training run records which classes load and link, and later runs start with them ready. JEP 514 (Java 25) simplifies creation to `-XX:AOTCacheOutput`, and JEP 515 (Java 25) adds method profiles so the JIT can start compiling sooner. - **Compact object headers** (JEP 519, Java 25) are a product option: `-XX:+UseCompactObjectHeaders`. They are off by default at 25 and on by default at 27. - **Collectors.** ZGC is generational only since Java 24 (JEP 490), and generational Shenandoah is a product feature at 25 (JEP 521). ## What can break or warn - **Security Manager** (JEP 486, Java 24): it can no longer be enabled, and at 25 `System.setSecurityManager` throws `UnsupportedOperationException`. - **JNI** (JEP 472, Java 24): loading and calling native code warns unless `--enable-native-access` covers it. - **`sun.misc.Unsafe`** (JEP 498, Java 24): memory-access methods warn on first use, after being deprecated for removal at Java 23. - **32-bit x86** (JEP 503, Java 25): the port is gone. ## What is still not final at 25 - **Previews:** structured concurrency (fifth preview), stable values, primitive types in patterns (third preview) and PEM encodings. - **Incubating:** the Vector API (tenth incubator). String templates are not on this list. They were previews in Java 21 and 22, then withdrawn, so Java 25 does not have them in any form. ## How to answer Pick what you would actually use: gatherers or scoped values in a library, the `synchronized` change if you run virtual threads, the ahead-of-time cache if startup matters. Say which release made each final, because 22, 24 and 25 all count here.
- Now that synchronized no longer pins virtual threads, should you change ReentrantLock code back?No. JEP 491 says code already migrated to `ReentrantLock` need not be reverted. For new code it recommends `synchronized` where practical and `java.util.concurrent.locks` when more flexibility is required. Either way, keep locks narrow and avoid blocking while holding one.
- How do you create and use an ahead-of-time cache at Java 25?Run a representative training run with `java -XX:AOTCacheOutput=app.aot -cp app.jar com.example.App`, then start production with `java -XX:AOTCache=app.aot -cp app.jar com.example.App`. Before Java 25 this took a separate record step and create step using `-XX:AOTMode`. The cache only helps as much as the training run resembles real startup.
- What do scoped values offer that ThreadLocal does not?A scoped value is immutable for its scope, and the binding ends when the scope ends, so there is no `remove()` to forget and no callee can overwrite it. JEP 506 also cites lower space and time costs, especially with virtual threads. Code that needs to mutate a per-thread value still uses `ThreadLocal`.
- Structured concurrency became final in Java 25, together with scoped values.
- String templates are still a preview feature at Java 25.
- Compact object headers are on by default at Java 25.
- At Java 25 the Security Manager can still be enabled with a system property.
- Compact source files are a separate beginner dialect of Java.
- A virtual thread can no longer be pinned at all at Java 25.
- Scoped values
- Java 25pre-release 20
- Stream gatherers
- Java 24pre-release 22
- Foreign Function & Memory API
- Java 22pre-release 17
- Synchronized without pinning virtual threads
- Java 24
- Ahead-of-time class loading and linking
- Java 24
Assumes JDK 27 as the baseline and compares Java 25 with Java 21; compact object headers are off by default at 25 and on at 27, and stable values became lazy constants at 26.
context
- JEPs in JDK 25 integrated since JDK 21openjdk.org
- JEP 491: Synchronize Virtual Threads without Pinningopenjdk.org
- JEP 485: Stream Gatherersopenjdk.org