[JVM] Six, local method interface

Hello, everyone, I am a pig who is dominated by cabbage.

A person who loves to study, sleepless and forgets to eat, is obsessed with the girl's chic, calm and indifferent coding handsome boy.

If you like my text, please follow the public account "Let go of this cabbage and let me come"

06-Local method interface

Insert picture description here

What is the local method

Simply put, a Native Method is an interface for Java to call non-Java code. A Native Method is a Java method: the implementation of the method is implemented in 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.”

When defining a native method, it does not provide an implementation body (somewhat like defining a Java
interface), because the implementation body is implemented outside by a non-Java language.

The role of the native interface is to integrate different programming languages ​​for Java, and its original intention is to integrate C/C++ programs.

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

Why use Native Method

Java is very convenient to use, but some levels of tasks are not easy to implement with Java, or when we are very concerned about the efficiency of the program, the problem comes.

  • Interact with outside the Java environment:

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

  • Interact with the operating system:

JVM supports the Java language itself and runtime libraries. It is the platform on which Java programs live. It consists of an interpreter (interpreting bytecode) and some libraries connected to native code. However, it is not a complete system after all, and it often relies on the support of some underlying systems. These underlying systems are often powerful operating
systems. **By using native methods, we can use Java to realize the interaction between jre and the underlying system, 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 native methods.

  • Sun ‘ s Java

**Sun's interpreter is implemented in C, which allows 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:
the setPriority() method of the class java. lang. Thread is implemented in Java, but it implements the call to the local method
setPriority0() in the class. This native method is implemented in C and is implanted inside the JVM. On the Windows 95 platform, this native method will eventually call the win32 SetPriority() API. This is the specific implementation of a local method directly provided by the JVM, and more often, the local method is provided by an external dynamic link library (external dynamic link library) and then called by the JVM.

status quo

At present, this method is used less and less. Unless it is hardware-related applications, such as driving printers through Java programs or managing production equipment through Java systems, it is relatively rare in enterprise-level applications. Because the communication between heterogeneous domains is now very developed, for example, you can use Socket communication, you can also use Web Service, etc., not much introduction.

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/112261015