- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between java method and native method?
Yes, native methods are methods whose implementation is written in a different language than Java. Following are the steps to use native methods.
Create a header file (.h file) for a C program.
Create C file
Create a DLL
In java code, declare the method as native, load the DLL using System.loadLibrary() method and call the method.
Example
Consider the following example −
Tester.java
public class Tester
public class Tester { public native int getValue(int i); public static void main(String[] args) { System.loadLibrary("Tester"); System.out.println(new Tester().getValue(2)); } }
Tester.c
#include <jni.h> #include "Tester.h" JNIEXPORT jint JNICALL Java_Tester_getValue(JNIEnv *env, jobject obj, jint i) { return i * i; }
Compile and run
javac Tester.java javah -jni Tester gcc -shared -fpic -o libTester.so -I${JAVA_HOME}/include \ -I${JAVA_HOME}/include/linux Tester.c java -Djava.library.path =. Tester
Output
4
- Related Articles
- What is difference between a Java method and native method
- What is the difference between a Java method and a native method?
- What is the difference between method hiding and method overriding in Java?
- What is the difference between method overloading and method hiding in Java?
- What is the difference between equals() method and == operator in java?
- Difference between Method Overloading and Method Overriding in Java
- Difference between constructor and method in Java
- Difference between == and equals() method in Java.
- What is the difference between a method and a function?
- Difference Between sleep() and wait() Method in Java
- What is the difference between operator and method on Python set?
- What are the differences between paint() method and repaint() method in Java?
- What are the differences between printStackTrace() method and getMessage() method in Java?
- What is difference between forEach() and map() method in JavaScript?
- What is the difference between function overriding and method hiding in C#?

Advertisements