After delving into the source code for the JRE library, I noticed a strangely common code structure, like this:
public int foo(double bar) { return foo0(bar); } private native int foo0(double bar);
What is the purpose of this code pattern, and why is it used instead of simply exposing the underlying native method as the public one?
Native methods are Java™ methods that start in a language other than Java. Native methods can access system-specific functions and APIs that are not available directly in Java. The use of native methods limits the portability of an application, because it involves system-specific code.
The native methods can be overridden just like any other methods, unless they are declared final .
The native version is just an implementation detail.
This pattern separates the public interface of the method from the actual implementation.
I see at least 5 reasons why this is useful:
You can probably find more advantages.
private native int foo(double bar);
So, eventually this has to call a C++ function for its implementation. In particular, it'll end up calling a function with a name something like:
Java_MyClass_foo
What happens if there are multiple native foo methods with different signatures? Complications. If you do it, Java will add type information into the name of the method it looks for. But it is simply easier if you stick to non-overloaded methods.
public int foo(double bar) { return foo0(bar); } private native int foo0(double bar);
foo0
has been given a unique name, there should never be a reason to add another foo0
. This keeps the implementation of the C++ simple, it never has to deal with mangled names. Even if foo eventually gains an overload, it will call foo1
instead of foo0
and the C++ JNI implementation won't have to deal with the extra complications of overloading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With