Quick revision
The short answer
The two proposals deserve different answers. The FFM API has been final since Java 22, is safer than JNI and needs no C glue code, and jextract can generate the bindings, so replacing JNI is a reasonable project once we benchmark it, test memory lifetimes with arenas and grant --enable-native-access. The Vector API is still an incubator module after many releases, waiting for Valhalla, so production use means accepting API changes; we first check whether the JIT already auto-vectorises the hot loop, and we decide per service with an exit plan.
What to remember
In this answer, we evaluated two Panama proposals differently. Replacing JNI with the FFM API, final since 22, is a sound project: less C glue, safer memory, jextract for bindings, with benchmarks, careful arena lifetimes and an explicit --enable-native-access grant. The Vector API is still incubating while it waits for Valhalla, so we first check whether C2 already auto-vectorises the loop, and only then allow it in an isolated class with a fallback, decided per service with an exit plan.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The two proposals deserve different answers. The FFM API has been final since Java 22, is safer than JNI and needs no C glue code, and jextract can generate the bindings, so replacing JNI is a reasonable project once we benchmark it, test memory lifetimes with arenas and grant --enable-native-access. The Vector API is still an incubator module after many releases, waiting for Valhalla, so production use means accepting API changes; we first check whether the JIT already auto-vectorises the hot loop, and we decide per service with an exit plan.
Why this question is asked
Both proposals come from the same place, Project Panama, and both promise speed or simplicity at the border between Java and the hardware. But one is a finished, supported API and the other is still an experiment, and treating them the same would be a mistake in either direction. Interviewers ask this scenario to see whether we can tell the difference and evaluate each with evidence. In this answer, we will recap both APIs, walk through the evaluation of the JNI replacement, then the Vector API, including the check that the JIT is not already doing the work, and end with how to decide and how to keep an exit open.
1A Quick Recap of the Two APIs
JNI, the Java Native Interface, is the old way to call C from Java. For every native function we write a Java native method and a C wrapper function that converts arguments, and we build that C code for each platform. The Foreign Function and Memory API (FFM) replaces this with pure Java: a Linker creates a method handle for a C function, and MemorySegment and Arena manage native memory with bounds and lifetime checks. It became final in Java 22.
The Vector API lets us write SIMD code, where one CPU instruction processes several values at once, such as eight floats in one AVX register, in portable Java. It lives in the jdk.incubator.vector module and has been incubating since Java 16, in large part because its final shape depends on Valhalla's value classes.
| Question | FFM replacing JNI | Vector API |
|---|---|---|
| Status | Final since Java 22 | Incubator module, many releases |
| Enable with | --enable-native-access (else warnings) | --add-modules jdk.incubator.vector |
| API stability | Normal compatibility promises | May change at any release |
| Main risk | Memory lifetime bugs in our code | Rewrites; JIT may already do it |
| Typical gain | Less C code, safer, often similar speed | Several times faster on hot numeric loops |
2Evaluating the JNI Replacement
The case for FFM is strong. There is no C glue code to write, build and ship per platform, so a whole category of build problems disappears. Native memory is accessed through MemorySegment, which checks bounds and fails cleanly after its arena is closed, instead of corrupting the heap. And jextract, a tool from the Panama project, reads the C header files and generates the Java bindings, so we do not write descriptors by hand.
Two things still need care. First, native access is restricted: since Java 22, using restricted FFM methods such as downcallHandle prints a warning unless the code is allowed with --enable-native-access=ALL-UNNAMED, or the module name, or the Enable-Native-Access manifest attribute; future releases are expected to tighten this. We add the flag deliberately and only for the module that needs it. Second, the C library itself is unchanged: if it keeps a pointer we passed and uses it after our arena closed, the FFM checks cannot see that. So we test memory handling around arena lifetimes, especially callbacks and buffers the library holds on to, under load and with tools like AddressSanitizer builds of the library where possible.
3Benchmarking the Change
We do not assume FFM is faster or slower. Downcalls through a method handle are well optimised by the JIT and usually close to JNI, but copying data between heap arrays and native segments can dominate. So we write a JMH benchmark for the real call pattern, for example compressing 10,000 images of the sizes we see in production, and compare JNI and FFM side by side on the same machine.
If FFM is within a few percent and removes the C build, it is worth it. If it is much slower, the usual cause is extra copying, which we fix by keeping data in native segments across calls rather than copying for every call.
4Evaluating the Vector API
For the Vector API the first question is not the API at all. HotSpot's C2 compiler already auto-vectorises many simple loops, turning them into SIMD instructions without any code change. So we profile the hot loop first and look at the generated code, or simply benchmark a plain loop against a Vector API version.
- Profile: is this loop really hot?JFR or async-profiler
- Does C2 already vectorise it?YesKeep the plain loopNoBenchmark a Vector API version
- Large gain in JMH on production data?YesAllow in one isolated class with fallbackNoKeep the plain loop
- Owner rechecks at each JDK upgradeexit plan: switch back to the plain loop
4 steps in order. Play them, or step through them one at a time.
If the plain loop is already vectorised, the API gains nothing and adds risk. The API shines where auto-vectorisation fails: loops with conditions, reductions, shuffles or lookups, such as parsing, hashing or similarity scoring. There, gains of several times are realistic. Even then, production use means the incubator module with a startup warning, and accepting that method names or classes may change at any release, so every JDK upgrade may require edits.
5Deciding Per Service, With an Exit Plan
Neither decision should be a platform-wide yes. For FFM, each service that replaces JNI goes through its own benchmark and memory tests, and the native access grant is recorded. The exit plan is easy: until the FFM path is proven, keep the JNI version buildable behind the same Java interface, and switch implementations with configuration.
For the Vector API, the exit plan is part of the design. The vector code sits in one class behind an interface, with a plain-loop implementation next to it that passes the same tests. If an upgrade breaks the incubator API, or the team that wrote it moves on, we switch to the plain loop and lose only speed. A named owner rechecks it at each JDK upgrade, and we revisit the whole decision when the API finally becomes final.
6Follow-up questions
Does FFM work on every platform where JNI worked?
The native linker supports the main platforms, such as Linux, macOS and Windows on x64 and AArch64; check your JDK for less common ones. If a platform is not supported, Linker.nativeLinker() throws, which we would see in the first test run.
What happens if we forget --enable-native-access?
On current JDKs the code still runs but prints a warning the first time a restricted method is used. The JDK's direction is to deny such access by default in a future release, so we add the grant now, explicitly and narrowly.
Why is the Vector API waiting for Valhalla?
Its vector types are designed to be value classes, objects without identity that the JVM can keep in registers. Until Valhalla delivers value classes, finalising the API would lock in a design that might need to change.
7Conclusion
In this answer, we evaluated two Panama proposals differently. Replacing JNI with the FFM API, final since 22, is a sound project: less C glue, safer memory, jextract for bindings, with benchmarks, careful arena lifetimes and an explicit --enable-native-access grant. The Vector API is still incubating while it waits for Valhalla, so we first check whether C2 already auto-vectorises the loop, and only then allow it in an isolated class with a fallback, decided per service with an exit plan.
Open with
I would treat them differently because their status is different. The FFM API has been final since Java 22, so replacing JNI with it is a normal engineering decision.
Then, if they want more
It removes the C glue code and per-platform builds, gives bounds-checked memory segments whose arena lifetime is enforced, and jextract can generate the bindings from headers.
I would still benchmark the real call pattern with JMH against the JNI version, test memory handling around arena lifetimes, especially buffers the C library keeps, and add --enable-native-access for that module, since restricted methods warn without it.
The Vector API is still an incubator module after many releases, largely waiting for Valhalla, so production use means --add-modules, a startup warning and possible API changes at every upgrade. Before even considering it, I would check whether C2 already auto-vectorises the hot loop, because for simple loops it often does.
If a benchmark on production data shows a large gain, I would allow it in one isolated class with a plain-loop fallback and a named owner. Both decisions are per service, with an exit plan.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 22, Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A team proposes replacing JNI code with the Foreign Function and Memory API, and another...": https://scrivio-interview-library.netlify.app/java/java-versions/f869b5b515/