JNI (Java Native Interface)
Understand the bridge between Java code and native libraries written in C/C++.
JNI Architecture
JNI defines a standard mechanism for Java code to call native methods and for native code to access Java objects.
It acts as the bridge between managed Java execution and host native libraries.
Native Methods
Native methods are declared in Java but implemented in native languages like C or C++.
These methods are used when platform capabilities or performance-critical behavior are not available in pure Java.
Native Libraries
Native libraries are compiled shared objects or DLLs loaded by the JVM for use through JNI.
They are commonly used for OS integration, graphics, networking, and high-performance system access.
JNI Lifecycle
The JNI lifecycle covers loading the library, linking native functions, and managing the Java-to-native transition safely.
The JVM ensures native code runs within a valid runtime context and handles thread and object management.
Calling C/C++ from Java
Java declares a native method and then provides a C/C++ implementation. The JVM maps method signatures and object references appropriately.
This allows Java programs to access system capabilities outside the standard library surface.
Calling Java from Native Code
Native code can invoke Java methods, access fields, and create Java objects using JNI.
This is useful for embedded systems, hybrid applications, and code that needs to interact with Java-managed objects from native layers.