Native Keyword in Java
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.
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: 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 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
For above program C code that is shared in DLL is as follows:
Example 1-B: Support to above example
Note: DLL is named as can be perceived from program 1A: NameOfDLLFile
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, 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 standard. Hence native strictfp combination is an illegal combination for methods.
- The main advantage of native keyword is performance improvement, but the main disadvantage of native keyword 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);
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...