native method in Java

Hirofumi Hara: https://blog.csdn.net/zmx729618/article/details/50779924

In Java development, you will encounter methods declared as native, such as: public native int hashCode(); This is a native method. Why do native methods exist? Java is not perfect. The shortcomings of Java are not only that the running speed is much slower than that of traditional C++, but Java cannot directly access the bottom layer of the operating system (such as system hardware, etc.). For this reason, Java uses native methods to extend Java programs. Function.
  
The native method can be compared to the interface between the Java program and the C program. The implementation steps are as follows:
  1. Declare the native method in Java, and then compile it.
  2. javahGenerate a .h file.
  3. Write a .cpp file to implement the native export method, which needs to include the .h file generated in the second step (note that it also includes the jni.h file with the JDK).
  4. Compile the .cpp file in the third step into a dynamic link library file.
  5. Use the System.loadLibrary()method to load the dynamic link library file generated in the fourth step in Java, and this native method can be accessed in Java.

JAVA's native method is applicable to the following situations:
  1. In order to use a certain feature of the underlying host platform, and this feature cannot be accessed through the JAVA API.
  2. In order to access an old system or use an existing library that is not written in JAVA.
  3. In order to speed up the performance of the program, the time-sensitive code is implemented as a local method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325341571&siteId=291194637