JVM_05_ Local method interface

  • In short, a Native Method is an interface for Java to call non java code.

  • A Native Method is a Java method that is implemented by a non Java language, such as C.

  • This feature is not unique to Java. Many other programming languages have this mechanism. For example, in C + +, you can use extern "C" to tell the C + + compiler to call a C function.

  • “A native method is a Java method whose implementation is provided by non-java code.” (local method is a Java method, and its specific implementation is the implementation of non java code)

  • When defining a Native Method, it does not provide an implementation (some like defining a Java interface), because its implementation is implemented outside by a non Java language.

  • The function of local interface is to integrate different programming languages for Java. Its original intention is to integrate C/C + + programs.

Examples of local methods

getClass() method of Object class

public final native Class<?> getClass();

start() method of Thread class

    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

    private native void start0();

Custom Native method

public class IHaveNatives{
    public native void methodNative1(int x);
    public native static long methodNative2();
    private native synchronized float methodNative3(Object o);
    native void methodNative4(int[] ary) throws Exception;
}

The identifier native can be used with other java identifiers, except abstract

Why use Native Method?

Java is very convenient to use. However, it is not easy to implement some level tasks in Java, or we are very concerned about the efficiency of the program.

Diplomatic interaction with Java environment

Sometimes Java applications need to interact with the environment outside Java, which is the main reason for the existence of local methods. You can think about the situation when Java needs to exchange information with some underlying systems, such as the operating system or some hardware.
Local method is such a communication mechanism: it provides us with a very concise interface, and we don't need to understand the cumbersome details outside Java applications.

Interaction with operating system

JVM supports the Java language itself and runtime library. It is the platform on which Java programs depend. It is composed of an interpreter (Interpreting bytecode) and some libraries connected to local code.
However, it is not a complete system after all. It often depends on the support of an underlying system. These underlying systems are often powerful operating systems.
By using local methods, we can realize the interaction between jre and the underlying system in Java, and even some parts of the JVM are written in c.
Also, if we want to use some features of the operating system that the Java language itself does not provide encapsulation, we also need to use local methods.

Sun's Java

Sun's interpreter is implemented in C, which enables it to interact with the outside world like some ordinary C. jre is mostly implemented in Java, and it also interacts with the outside world through some local methods.
For example: class java The setpriority () method of lang. thread is implemented in Java, but it calls the local method setpriority () in this class. This local method is implemented in C and embedded in the JVM. On the platform of Windows 95, this local method will eventually call Win32 setPriority() ApI.
This is the specific implementation of a local method, which is directly provided by the JVM. More often, the local method is provided by an external dynamic link library and then called by the JVM.

present situation

At present, this method is used less and less, except for hardware related applications, such as driving printers through Java programs or managing production equipment through Java systems, which are rare in enterprise applications. Because now the communication between heterogeneous fields is very developed, such as Socket communication, Web Service and so on.

Keywords: Java jvm

Added by limpo on Thu, 03 Feb 2022 13:44:56 +0200