Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

  • native methods in Java are methods declared in a Java class but implemented in a native language like C or C++ using the Java Native Interface (JNI).
  • They are used when Java code needs to interact with hardware, operating system, or performance-critical operations that Java cannot handle efficiently.
  • The native keyword is used to declare such methods, and they are typically loaded from a shared library (e.g., .dll on Windows or .so on Unix).

concepts

  • Native Method Declaration - use the native keyword in the method signature, with no implementation in Java.
  • JNI: The Java Native Interface provides the framework to call native code and pass data between Java and the native environment.
  • Shared Library: The native code is compiled into a shared library, which is loaded into the JVM using System.loadLibrary().
  • Instance Context: For instance native methods, the native code receives a reference to the Java object (this) via the JNIEnv pointer.