native keyword in Java

native keyword is used for java methods.

A method with native keyword indicates that it is implemented in platform-dependent code, typically written in another programming language such as C, C++.

The Java Native Interface (JNI) 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.

The body of a native method is given as a semicolon only, indicating that the implementation is omitted, instead of a block.

Some the native methods in Runtime Class

  • public native int availableProcessors();
  • public native long freeMemory();
  • public native long totalMemory();
  • public native long maxMemory();
  • public native void gc();

Some of the native  methods in System Class that we use on regular basis.

  • public static native long currentTimeMillis();
  • public static native long nanoTime();

Leave a Reply

Your email address will not be published. Required fields are marked *