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.
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
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);
Similar Reads
var keyword in Java
The var reserved type name (not a Java keyword) was introduced in Java 10. Type inference is used in var keyword in which it detects automatically the datatype of a variable based on the surrounding context. The below examples explain where var is used and also where you can't use it. 1. We can decl
4 min read
Java Keywords
In Java, keywords are the reserved words that have some predefined meanings and are used by the Java compiler for some internal process or represent some predefined actions. These words cannot be used as identifiers such as variable names, method names, class names, or object names. Now, let us go t
5 min read
abstract keyword in java
In Java, abstract is a non-access modifier in java applicable for classes, and methods but not variables. It is used to achieve abstraction which is one of the pillars of Object Oriented Programming(OOP). Following are different contexts where abstract can be used in Java. Characteristics of Java Ab
7 min read
Important Keywords in Java
Keywords refer to the reserved set of words of a language. These are used for some predefined actions. abstract: It is a non-access modifier applicable for classes and methods. It is used to achieve abstraction. For more, refer to abstract keyword in javaenum: It is used to define enum in Javainstan
2 min read
instanceof Keyword in Java
In Java, instanceof is a keyword used for checking if a reference variable contains a given type of object reference or not. Following is a Java program to show different behaviors of instanceof. Henceforth it is known as a comparison operator where the instance is getting compared to type returning
4 min read
JRE in Java
Java Runtime Environment (JRE) is an open-access software distribution that has a Java class library, specific tools, and a separate JVM. In Java, JRE is one of the interrelated components in the Java Development Kit (JDK). It is the most common environment available on devices for running Java prog
4 min read
Java KeyListener in AWT
The Java KeyListener in the Abstract Window Toolkit (AWT) is a fundamental tool for achieving this. The KeyListener Interface is found in "java.awt.event" package. In this article, we'll explore what the KeyListener is, and its declaration methods, and supply examples with explanatory comments. Java
3 min read
Java.util Package in Java
Java.util PackageIt contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).Following are the Important Classes in Java.util package
4 min read
StringJoiner merge() method in Java
The merge(StringJoiner other) of StringJoiner adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect.Syntax: public StringJoiner merge(StringJoiner other) Parameters: This method accepts
3 min read
StringJoiner length() method in Java
The length() of StringJoiner is used to find out the length of the StringJoiner in characters. It returns the length of the String representation of this StringJoiner. Note that if no add methods have been called, then the length of the String representation (either prefix + suffix or emptyValue) wi
2 min read