Skip to content
Related Articles

Related Articles

Native Keyword in Java

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 22 Apr, 2022

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:

  1. Write java code
  2. Compile the java code.
  3. Create C header(.h file)
  4. Create C stubs file (using tool: Java HEdge)
  5. Write C code
  6. Create a shared code library (DLL)
  7. 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 

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 above program C code that is shared in DLL is as follows:

Example 1-B: Support to 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 1
// Main driver method
int main()
{
 
    test(10);
 
    return;
}
 
// Method 2
// Native
void test()
{
    int var;
    cout << var;
}


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, 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 review-team@geeksforgeeks.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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!