The native keyword is applied to a method to indicate that the method is implemented in native code using JNI (Java Native Interface). native is a modifier applicable only for methods and we can’t apply it anywhere else. The methods which are implemented in C, C++ are called as native methods or foreign methods.
The main objectives of native keyword are:
- To improve performance of the system.
- To achieve machine level/memory level communication.
- To use already existing legacy non-java code.
Pseudo code to use native keyword in java:
Important points about native keyword:
- For native methods implementation is already available in old languages like C, C++ and we are not responsible to provide implementation. Hence native method declaration should end with ; (semi-colon).
- We can’t declare native method as abstract.
- We can’t declare native method as strictfp because there is no guarantee that old languages (C, C++) follow IEEE 754 standard. Hence native strictfp combination is illegal combination for methods.
- The main advantage of native keyword is improvement in performance but the main disadvantage of native keyword is that it breaks platform independent nature of java.
Declaring Native Methods: In this section we explain how to declare a native method in Java and how to generate the corresponding C/C++ function prototype.
private native String getLine(String prompt);
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 contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Green vs Native Threads and Deprecated Methods in Java
- How to Integrate Facebook Audience Network (FAN) Native Ads in Android?
- Comparison of static keyword in C++ and Java
- volatile keyword in Java
- transient keyword in Java
- strictfp keyword in java
- final keyword in java
- Super Keyword in Java
- abstract keyword in java
- static keyword in java
- return keyword in Java
- Protected keyword in Java with examples
- Using the super Keyword to Call a Base Class Constructor in Java
- var keyword in Java
- super keyword for Method Overloading in Java
- Usage of Enum and Switch keyword in Java
- Usage of Break keyword in Java
- Access Super Class Methods and Instance Variables Without super Keyword in Java
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
Improved By : gowtham_yuvaraj