What is the native keyword in Java for?

后端 未结 10 1423
粉色の甜心
粉色の甜心 2020-11-22 10:52

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?

相关标签:
10条回答
  • 2020-11-22 11:20

    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.

    0 讨论(0)
  • 2020-11-22 11:22

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

    0 讨论(0)
  • 2020-11-22 11:23

    Java native method provides a mechanism for Java code to call OS native code, either due to functional or performance reasons.

    Example:

    • java.lang.Rutime (source code on github) contains the following native method definition
    606  public native int availableProcessors();
    617  public native long freeMemory();
    630  public native long totalMemory();
    641  public native long maxMemory();
    664  public native void gc();
    

    In the corresponding Runtime.class file in OpenJDK, located in JAVA_HOME/jmods/java.base.jmod/classes/java/lang/Runtime.class, contains these methods and tagged them with ACC_NATIVE (0x0100), and these methods do not contain the Code attribute, which means these method do not have any actual coding logic in the Runtime.class file:

    • Method 13 availableProcessors: tagged as native and no Code attribute
    • Method 14 freeMemory: tagged as native and no Code attribute
    • Method 15 totalMemory: tagged as native and no Code attribute
    • Method 16 maxMemory: tagged as native and no Code attribute
    • Method 17 gc: tagged as native and no Code attribute

    The in fact coding logic is in the corresponding Runtime.c file:

    42  #include "java_lang_Runtime.h"
    43
    44  JNIEXPORT jlong JNICALL
    45  Java_java_lang_Runtime_freeMemory(JNIEnv *env, jobject this)
    46  {
    47      return JVM_FreeMemory();
    48  }
    49
    50  JNIEXPORT jlong JNICALL
    51  Java_java_lang_Runtime_totalMemory(JNIEnv *env, jobject this)
    52  {
    53      return JVM_TotalMemory();
    54  }
    55
    56  JNIEXPORT jlong JNICALL
    57  Java_java_lang_Runtime_maxMemory(JNIEnv *env, jobject this)
    58  {
    59      return JVM_MaxMemory();
    60  }
    61
    62  JNIEXPORT void JNICALL
    63  Java_java_lang_Runtime_gc(JNIEnv *env, jobject this)
    64  {
    65      JVM_GC();
    66  }
    67  
    68  JNIEXPORT jint JNICALL
    69  Java_java_lang_Runtime_availableProcessors(JNIEnv *env, jobject this)
    70  {
    71      return JVM_ActiveProcessorCount();
    72  }
    

    And these C coding is compiled into the libjava.so (Linux) or libjava.dll (Windows) file, located at JAVA_HOME/jmods/java.base.jmod/lib/libjava.so:

    Reference

    • Java Native Methods Essentials
    0 讨论(0)
  • 2020-11-22 11:32

    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

    0 讨论(0)
  • 2020-11-22 11:32
    • native is a keyword in java, it indicates platform dependent.
    • native methods are acts as interface between Java(JNI) and other programming languages.
    0 讨论(0)
  • 2020-11-22 11:37

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

    0 讨论(0)
提交回复
热议问题