Native methods in java – Java Native Interface (JNI)
java.lang.Object class defined like
1 |
public native int hashCode(); // without any body |
Now, in spite of having two years of hands on experience in java and related technologies, I didn’t know that it is a native method. This made me dig deeper and explore the native interface API provided by java. So, there goes
A brief introduction
Simply put, a native method is the Java interface to non-Java code. It is Java’s link to the “outside world”. Native methods are written in a “native” language other than java. This would normally use C to write the methods, but a lot of other languages, such as python allows you to write methods, too. Native methods are implemented mostly in C and compiled to native code which runs directly on the machine. This is in contrast to normal methods, which are implemented in Java and compiled to Java byte code, which is executed by the Java Virtual Machine (JVM). To use the native code, we need the Java Native Interface api.

JNI
Advantages of using Native methods
1 – Performance
In a few rare cases, proven performance benefit (because you know your particular C compiler can optimise some particular code better than the JVM’s JIT compiler)
2 – Access to platform specific infrastructure
When platform specific infrastructure (facilities of the operating system) are required, which is not provided in standard java libraries. This can be the low level stuff, i.e. in case of hashCode native method, the address of the object in memory.
3 – Reusability
When some library or code already written in C/C++ is required, and you want to use it without porting it to java code.
4 – MultiLinguality
Most real software systems are multilingual; that is, they consist of components developed in different programming languages. Developers can reuse existing code modules, and can also mix and match strengths of different languages. As an example, imagine that a developer intends to perform data compression in Java. Instead of writing all the code for data compression from scratch, he can reuse the existing ZLib C library and write a thin interface between Java and C, via the Java Native Interface (JNI).
Disadvantages
1 – Possible loss of safety
The safety and security of the JVM is lost, i.e. your program might crash or have security holes due to bugs in the native code.
2 – Low portability
The code is not portable instead it is used through an interface.
3 – Low Codeability
The native code is not in java which causes the codeability to be low. Now to manage that code you will need a person having both java and c/c++ skills.
4 – JNI call overhead
Compared to a pure Java method call, calling a user-written native method usually has a significant overhead. The reasons for this are as much to do with optimisations that the JVM can’t make compared to regular Java methods. JVM doesn’t know much about the native method. For example, it has to assume that all of the parameters passed in are always used.
After a quick review, the benefits of using native code may need to be weighed up against the resulting loss in portability, security and codability.
References
(official JNI documentation) http://docs.oracle.com/javase/7/docs/technotes/guides/jni/ (others) http://en.wikipedia.org/wiki/Java_hashCode() http://www.javamex.com/tutorials/jni/ http://www.cse.lehigh.edu/~gtan/projects/multilingual.html If you liked the article, help me by sharing it with your friends on social media using the icons given below.
Comments