What is difference between a Java method and native method

Java 8ProgrammingObject Oriented Programming

This article will help you understand all differences between a Java method and a native method.

Function / Method

A program module (a part of the program) used simultaneously at different instances in a program to perform specific task is known as Method or Function. It can be regarded as a black box that is capable of returning an output (obtained as per execution of the codes written inside). Similarly, all the methods available in Java class act as a black box. On providing values (arguments) to a method, it processes the code available within it and returns the output.

Advantages of Using a Method

  • It hides low-level inner code details from its users. External use of methods may be allowed without exposing inner details.

  • It reuses the segment of class codes, as and when necessary, by simply using the method name.

  • It divides a complex computational task into a collection of smaller methods so that problem solving becomes easier, object-specific and modular.

Defining a Method

The general form of defining a method is as shown below −

<Access specifier> <Return Type> <Method Name> (Parameter List){
   Statement
   ______________
   ______________
   return (value);
}

Example

public int Addition(int a, int b){
   int sum = 0;
   sum + = a + b;
   return (sum);
}

Here public is the access specifier, int is the return type, Addition is the method name, int a and int b are parameters.

Native Method

The Native method is a type of Java method which starts in a language other than Java. This can access system specific functions and APIs which aren't directly available in Java.

Using native methods leads to limiting the portability of an application, as it involves system specific code. They can either be new code statements or code statements calling existing native code.

There is a need to interoperate with the Java Virtual machine while running a native method. This is done by the Java Native Interface (JNI) which facilitates this interoperability in a platform independent way.

The JNI is a set of interfaces which allows a native method to interoperate with the JVM using several ways. Like for example, the JNI can include interfaces that creates objects, calls methods, gets fields, set fields, manipulates arrays, manipulates strings and perform process exceptions.

Uses of Native Method

One should use Native Methods in cases where Java programming language is insufficient to meet the programming demands. This is because using them leads to limiting the portability of an application, as it involves system specific code. Below given are some conditions where one should use Native methods −

  • To access system functions which aren’t accessible using JAVA programming language.

  • Implementing performance specific methods that benefits significantly from a native implementation.

  • To create an interface of existing APIs that allows JAVA to call other APIs.

Steps to Create a Native Method

Step 1 − Write a Java Code and Compile it.

Step 2 − Create a C header (.h) file.

Step 3 − Create C stubs file using tool  Java − HEdge.

Step 4 − Write the C Code.

Step 5 − Create a shared Code Library or DLL

Step 6 − Run the application

Differences between Java Method and Native Method

SL.NO Java Method Native Method
1 It is written specifically in Java Language. It is written in a language other than Java, like C.
2 There are no separate keywords to declare a method. To declare a native method, one must use the ‘native’ keyword.
3 Cannot access system specific functions. Used for accessing system specific functions.
4 Requires Java Virtual Machine to execute. Required Java Native Interface to execute.
5 Doesn’t limit portability of an application. Limits portability of an application.
6 Example − public int getValue (int n) { } Example − public native int getValue (int n);
raja
Updated on 05-Sep-2022 12:39:32

Advertisements