From Effective Java 2/e by Joshua Bloch
Java Native Interface (JNI)
Allows Java applications to call native methods, which are special methods written in native programming languages such as C or C++.
Three main usage
- They provided access to platform-specific facilities such as registries and file locks
- They provided access to libraries of legacy code, which could in turn provide access to legacy data
- Native methods were used to write performance-critical parts of applications in native languages for improved performance
It is rarely advisable to use native methods for improved performance
- For most tasks, it is now possible to obtain comparable perfor- mance without resorting to native methods
- Because native languages are not safe, applications using native methods are no longer immune to memory corruption errors
- Because native languages are platform dependent, applications using native methods are far less portable
- Applications using native code are far more difficult to debug
- There is a fixed cost associated with going into and out of native code, so native methods can decrease performance if they do only a small amount of work
- Native methods require “glue code” that is difficult to read and tedious to write