0Pricing
Java Academy · Lesson

Why FFM over JNI

Safer native interop.

Why FFM over JNI is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Calling Native Code

Java sometimes needs to call C libraries or work with off-heap memory. The classic way was the Java Native Interface (JNI). The modern way is the Foreign Function and Memory API (FFM), finalized in Java 22 (JEP 454).

This lesson explains why FFM is the better choice.

What JNI Required

JNI was painful:

  • Write C glue code with awkward JNIEXPORT signatures
  • Compile a native shared library per platform
  • Manually convert between Java and C types
  • Easy to crash the JVM with a mistake

FFM Is Pure Java

FFM lets you call native functions and access native memory entirely from Java. No C glue, no separate compilation step, no hand-written marshalling.

You describe the native function's signature in Java and invoke it through a method handle.

The Key Packages

Everything lives in java.lang.foreign. The headline types are:

  • Linker and SymbolLookup to find and bind functions
  • MemorySegment for native memory
  • Arena for deterministic lifetime
  • MemoryLayout and FunctionDescriptor to describe shapes

Safer by Design

FFM is far safer than JNI:

  • Bounds-checked memory access
  • Lifetime tied to an Arena, so use-after-free is caught
  • Confinement prevents unsafe cross-thread access

Mistakes throw Java exceptions rather than crashing the VM.

A Taste of the API

This sketch finds the C strlen function and prepares a handle to it. The point is that this is all ordinary Java code.

import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;

public class Main {
    public static void main(String[] args) {
        Linker linker = Linker.nativeLinker();
        SymbolLookup stdlib = linker.defaultLookup();
        MethodHandle strlen = linker.downcallHandle(
            stdlib.find("strlen").orElseThrow(),
            FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.ADDRESS));
        System.out.println("Bound a handle to strlen: " + strlen);
    }
}

Performance

FFM downcalls are competitive with JNI and often faster, because the JIT can inline and optimize the generated stubs. There is no per-call C trampoline to cross.

Enabling Native Access

Because native code can be dangerous, FFM may warn or require an opt-in. You grant access at launch with --enable-native-access=ALL-UNNAMED (or a specific module name) to silence warnings.

Replacing sun.misc.Unsafe

FFM (with the Memory API) is also the sanctioned replacement for the long-deprecated sun.misc.Unsafe off-heap operations. Libraries that managed native buffers manually can migrate to safe, supported APIs.

Tooling: jextract

For large C libraries, the companion tool jextract reads a C header file and auto-generates the Java FFM bindings. You skip writing descriptors by hand.

It is the productivity counterpart to FFM, much like a code generator.

When to Use FFM

Reach for FFM when you must:

  • Call an existing C/C++ library
  • Interoperate with the OS at a low level
  • Manage large off-heap buffers efficiently

For pure-Java work, you never need it.

Quick Check

Recall the headline advantage over JNI.

Recap

You learned why FFM beats JNI:

  • Pure Java, no C glue or extra compilation
  • Safe: bounds checks, Arena lifetimes, confinement
  • Competitive or better performance
  • Replaces sun.misc.Unsafe; pairs with jextract

Next: managing native memory with MemorySegment and Arena.

Frequently asked questions

Is the “Why FFM over JNI” lesson free?

Yes — the full text of “Why FFM over JNI” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Why FFM over JNI”?

Safer native interop. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why FFM over JNI” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why FFM over JNI
  2. MemorySegment and Arena
  3. Downcall Handles
  4. Layouts and Structs
← Back to Java Academy