So far we have spent time focusing on Java as a standalone, powerful programming language. One of its greatest strengths is that it’s also able to integrate with other native programming languages through tools like JNI. JNI stands for Java Native Interface. It is a powerful tool that bridges together a Java program and native languages. Native languages are coding languages that are innately understood by the computer. Code written in a native language does not need to be altered for a processor in a computer to interpret it. Some examples of native languages include C and C++. Further along in this module, we will see how C++ code can be integrated into Java programs.
While implementing JNI may not always be the fastest or most cost-effective way to implement methods in a program it has certain benefits that programmers may choose to apply. Some of these benefits include:
- Being able to avoid code redundancy by simply calling a native language function, instead of trying to rewrite it in Java.
- Being able to override Java’s dynamic abilities to determine things such as memory allocation manually.
- Being able to implement platform-specific features.
- Being able to access functionality that is outside the scope of the Java Standard Library.
For the rest of this exercise, we will learn more about the relevance of C++ to JNI, how to implement third-party APIs using JNI, and how to implement JNI within your own environments.
Take a look at the image in this exercise to understand how JNI connects Java to native languages!