[JAVA] Item 66: Use native methods judiciously

66. Native methods should be used with caution

native method

You can call methods written in C or C ++ using the Java Native Interface (JNI).

Historically, native methods have three uses.

It is legitimate to use native methods to access platform-specific features. That said, Java has matured, so it has most of its features. For example, Java 9 allows you to access OS processes. Also, if there is a library in the native library that is not in Java, you can use the native method.

I rarely use native methods for performance. It seems that such a thing happened in early Java, but the performance has not changed so much due to the evolution of the JVM.

Disadvantages of native methods

The native method has major drawbacks.

In other words, think carefully and use the native method.

Recommended Posts

Item 66: Use native methods judiciously
Item 52: Use overloading judiciously
Item 53: Use varargs judiciously
Item 45: Use streams judiciously
Item 83: Use lazy initialization judiciously
Item 67: Optimize judiciously
Item 30: Favar generic methods
Item 55: Return optionals judiciously
Item 26: Don't use raw types
Use native code on Android
Item 88: Write readObject methods defensively
Item 32: Combine generics and varargs judiciously
Item 59: Know and use the libraries
How to use class methods [Java]
Item 40: Consistently use the Override annotation