Home » Java » What is the native keyword in Java for?

What is the native keyword in Java for?

Posted by: admin November 2, 2017 Leave a comment

Questions:

While playing this puzzle (It’s a Java keyword trivia game), I came across the native keyword.

What is the native keyword in Java used for?

Answers:

The native keyword is applied to a method to indicate that the method is implemented in native code using JNI (Java Native Interface).

Questions:
Answers:

It marks a method, that it will be implemented in other languages, not in Java. It works together with JNI (Java Native Interface).

Native methods were used in the past to write performance critical sections but with Java getting faster this is now less common. Native methods are currently needed when

  • You need to call a library from Java that is written in other language.

  • You need to access system or hardware resources that are only reachable from the other language (typically C). Actually, many system functions that interact with real computer (disk and network IO, for instance) can only do this because they call native code.

See Also
Java Native Interface Specification

Questions:
Answers:

Minimal example to make things clearer:

Main.java:

public class Main {
    public native int square(int i);
    public static void main(String[] args) {
        System.loadLibrary("Main");
        System.out.println(new Main().square(2));
    }
}

Main.c:

#include <jni.h>
#include "Main.h"

JNIEXPORT jint JNICALL Java_Main_square(
    JNIEnv *env, jobject obj, jint i) {
  return i * i;
}

Compile and run:

sudo apt-get install build-essential openjdk-7-jdk
export JAVA_HOME='/usr/lib/jvm/java-7-openjdk-amd64'
javac Main.java
javah -jni Main
gcc -shared -fpic -o libMain.so -I${JAVA_HOME}/include \
  -I${JAVA_HOME}/include/linux Main.c
java -Djava.library.path=. Main

Output:

4

Tested on Ubuntu 14.04 AMD64. Also worked with Oracle JDK 1.8.0_45.

Example on GitHub for you to play with.

Interpretation:

It allows you to:

  • call a compiled dynamically loaded library (here written in C) with arbitrary assembly code from Java
  • and get results back into Java

This could be used to:

  • write faster code on a critical section with better CPU assembly instructions (not CPU portable)
  • make direct system calls (not OS portable)

with the tradeoff of lower portability.

It is also possible for you to call Java from C, but you must first create a JVM in C: How to call Java functions from C++?

Example in the OpenJDK 8

Let’s find find where Object#clone is defined in jdk8u60-b27.

We will conclude that it is implemented with a native call.

First we find:

find . -name Object.java

which leads us to jdk/src/share/classes/java/lang/Object.java#l212:

protected native Object clone() throws CloneNotSupportedException;

Now comes the hard part, finding where clone is amidst all the indirection. The query that helped me was:

find . -iname object.c

which would find either C or C++ files that might implement Object’s native methods. It leads us to jdk/share/native/java/lang/Object.c#l47:

static JNINativeMethod methods[] = {
    ...
    {"clone",       "()Ljava/lang/Object;",   (void *)&JVM_Clone},
};

JNIEXPORT void JNICALL
Java_java_lang_Object_registerNatives(JNIEnv *env, jclass cls)
{
    (*env)->RegisterNatives(env, cls,
                            methods, sizeof(methods)/sizeof(methods[0]));
}

which leads us to the JVM_Clone symbol:

grep -R JVM_Clone

which leads us to hotspot/src/share/vm/prims/jvm.cpp#l580:

JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
    JVMWrapper("JVM_Clone");

After expanding a bunch of macros, we come to the conclusion that this is the definition point.

Questions:
Answers:

Straight from the Java Language Specification:

A method that is native is implemented in platform-dependent code, typically written in another programming language such as C, C++, FORTRAN,or assembly language. The body of a native method is given as a semicolon only, indicating that the implementation is omitted, instead of a block.

Questions:
Answers:

As SLaks answered, the native keyword is for calling native code.

It also used by GWT for implementing javascript methods.

Questions:
Answers:

functions that implement native code are declared native.

The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be called by, native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.

http://en.wikipedia.org/wiki/Java_Native_Interface

Questions:
Answers:

native is a keyword in java , which is used to make unimplemented structure(method) like as abstract but it would be a platform dependent such as native code and execute from native stack not java stack.

Questions:
Answers:

NATIVE is Non access modifier.it can be applied only to METHOD.
It indicates the PLATFORM-DEPENDENT implimentation of method or code.

Questions:
Answers:

The native keyword is used to declare a method which is implemented in platform-dependent code such as C or C++. When a method is marked as native, it cannot have a body and must ends with a semicolon instead. The Java Native Interface (JNI)specification governs rules and guidelines for implementing native methods, such as data type conversion between Java and the native application.

The following example shows a class with a method declared as native

public class NativeExample {

public native void fastCopyFile(String sourceFile, String destFile);

}

Disadvantages of native methods:

  • Native methods are stored with .DLL (dynamic link library) file extensions in Windows. DLL files are machine dependent. Thus, native methods are not portable.

  • Since native methods use machine code, it can lead to serious security issues.

Questions:
Answers:
  • native is a keyword in java, it indicates platform dependent.
  • native methods are acts as interface between Java(JNI) and other programming languages.
Questions:
Answers:

The native keyword is used to declare a method which is implemented in platform-dependent code such as C or C++. When a method is marked as native, it cannot have a body and must ends with a semicolon instead. The Java Native Interface (JNI)specification governs rules and guidelines for implementing native methods, such as data type conversion between Java and the native application.

The following example shows a class with a method declared as native:

public class NativeExample {

public native void fastCopyFile(String sourceFile, String destFile);

}