The native keyword in Java is applied to a method to indicate that the method is implemented in native code using JNI (Java Native Interface). The native keyword is a modifier that is applicable only for methods, and we can’t apply it anywhere else. The methods which are implemented in C, C++ are called native methods or foreign methods.
The native modifier indicates that a method is implemented in platform-dependent code, often seen in C language. Native modifier indicates that a method is implemented in platform-dependent code, often in C.
Main objectives of the native keyword
- To improve the performance of the system.
- To achieve machine level/memory level communication.
- To use already existing legacy non-java code.
Conclusion: Java application scan call code is written in C, C++, or assembler.
Create Native Method
The steps for the creation of native methods are as follows:
- Write java code
- Compile the Java code.
- Create C header(.h file)
- Create C stubs file (using tool: Java HEdge)
- Write C code
- Create a shared code library (DLL)
- Run application
Implementation of Native keyword in Java
Let us first take random Java code that contains the native method and later we will be compiling it. We are done with the above two steps. For steps 3 and 4 we will be using the existing .exe known as java HEdge” in order to create a C header and C stub file.
Now we will insert(write) our C code(or use) and later using DLL, we will be creating objects of the same inside our application( Main example1A) and later calling the native methods thereby within the java program.
Example 1-A: Application
Java
// Java Program to Illustrate Native Keyword // Inside DLL named: NameOfDLLFile // Main class // NativeDemo class GFG { // Method 1 public static void main(String[] args) { int var; // Here we will not be having body of this method // in our java code here NameOfDLLFile obj = new NameOfDLLFile(); obj.var = null ; System.out.println( "Before native method: var = " + var); obj.test(); System.out.println( "After native method: var = " + var); } // Native method public native void test() { static { // We will be loading body from DLL file // It has to be present in DLL file System.loadLibrary( "NameOfDLLFile" ); // Above C code in loaded in the JVM } } } |
For the above program C code that is shared in DLL is as follows:
Example 1-B: Support to the above example
C++
// C++ Program to Be Shared In DLL to Illustrate // Native Method in Java // Importing required libraries #include <iostream> using namespace std; // Method 2 // Native void test( int var) { cout << var; } // Method 1 // Main driver method int main() { test(10); return 1; } |
Note: DLL is named as can be perceived from program 1A: NameOfDLLFile
Output:
Before native method: var = null
After native method: var = 10
Do remember that there are certain important points about native keywords, which are as follows:
- For native methods, implementation is already available in old languages like C, and C++ and we are not responsible for providing an implementation. Hence native method declaration should end with ; (semi-colon).
- We can’t declare a native method as abstract.
- We can’t declare a native method as strictfp because there is no guarantee that old languages (C, C++) follow IEEE 754 standards. Hence native strictfp combination is an illegal combination for methods.
- The main advantage of native keywords is performance improvement, but the main disadvantage of native keywords is that it breaks the platform-independent nature of Java.
Note: Do go through strictfp keyword of java as is one of the concept which even very good java developer is unaware of.
In this section, we explain how to declare a native method in Java and how to generate the corresponding C/C++ function prototype.
Syntax: Declaring Native Methods
private native String getLine(String prompt);
Syntax: From the Native Language Side
javah -jni Prompt
JNIEXPORT jstring JNICALL Java_Prompt_getLine(JNIEnv *, jobject, jstring);