Home >Java >javaTutorial >Java native method Native Method details

Java native method Native Method details

高洛峰
高洛峰Original
2017-02-28 15:33:121344browse

Detailed introduction to Java native method Native Method

1. What is Native Method

Simply put, a Native Method is an interface for Java to call non-java code . A Native Method is a Java method whose implementation 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, the implementation body is not provided ( Some like defining a java interface), because its implementation is implemented externally by non-java languages. , an example is given below:

 public class IHaveNatives
  {
   native public void Native1( int x ) ;
   native static public long Native2() ;
   native synchronized private float Native3( Object o ) ;
   native void Native4( int[] ary ) throws Exception ;
  }

The declarations of these methods describe what some non-java code looks like in these java codes ( view).

The identifier native can be used with all other java identifiers, except abstract. This is reasonable, because native implies that these methods have implementations, but these implementations are non-Java, but abstract clearly indicates that these methods have no implementation. When native is used with other Java identifiers, its meaning is no different from non-Native Method. For example, native static indicates that this method can be called directly without generating an instance of the class. This is very convenient, for example, when you want to use a native method to call it. A C class library. The third method above uses native synchronized. The JVM will execute a synchronization lock mechanism before entering the implementation of this method (just like Java's multi-threading.)

A native method can return any Java types, including non-basic types, can also be used for exception control. The implementation of these methods can create an exception and throw it, which is very similar to Java methods. When a native method receives some non-basic types such as Object or an integer array, the method can access the internals of these non-basic types, but this will make the native method dependent on the implementation of the Java class you are accessing. One thing to keep in mind is that we can access all Java features in a native implementation of a native method, but this depends on the implementation of the Java features you access, and doing so is far inferior to using those in the Java language. Features are convenient and easy.

The existence of native methods will not have any impact on other classes calling these native methods. In fact, other classes calling these methods do not even know that they are calling a native method. The JVM will control all the details of calling native methods. We need to pay attention to the situation when we declare a local method as final. Method bodies implemented in Java may experience efficiency improvements due to inlining when they are compiled. However, it is questionable whether a native final method can also obtain such benefits, but this is only a code optimization issue and has no impact on functional implementation.

If a class containing a local method is inherited, the subclass will inherit this local method and can override this method in Java language (this seems a bit strange), the same if a local method Identified by fianl, it cannot be overridden after being inherited.
Local methods are very useful because they effectively extend jvm. In fact, the java code we write has already used local methods. In the implementation of sun's java concurrency (multi-threading) mechanism, many of them are related to the operating system. All touch points use native methods, which allows Java programs to transcend the boundaries of the Java runtime. With native methods, a Java program can do any application-level task.

2. 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, Here comes the problem.

Interacting with outside the java environment:

Sometimes java applications need to interact with the environment outside java. This is the main reason why local methods exist. 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 methods are exactly such a communication mechanism: it provides us with a very simple interface, and we do not need to understand the cumbersome details outside of the Java application.

Interacting with the operating system:

The JVM supports the Java language itself and the runtime library. It is the platform on which Java programs rely. It consists of an interpreter (interpreting bytecode) and some libraries connected to local code. However, it is not a complete system after all. It often relies on the support of some underlying (underneath) 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. 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 like some ordinary C. Most of jre is implemented in java, and it also interacts with the outside world through some local methods. For example: the setPriority() method of class java.lang.Thread is implemented in java, but it calls the local method setPriority0() in the class. This native method is implemented in C and implanted inside the JVM. On the Windows 95 platform, this native method will eventually call the Win32 SetPriority() API. This is a specific implementation of a local method provided directly by the JVM. More often, the local method is provided by an external dynamic link library (external dynamic link library) and then called by the JVM.

3. How does JVM make Native Method run:

We know that when a class is used for the first time, the bytecode of this class will be loaded into memory , and will only be loaded once. A list of all method descriptors of the class is maintained at the entrance of the loaded bytecode. These method descriptors contain such information: where the method code is stored, what parameters it has, and the method descriptor (public). category) and so on.

If there is native in a method descriptor, this descriptor block will have a pointer to the implementation of the method. These implementations are in some DLL files, but they will be loaded into the address space of the Java program by the operating system. When a class with a native method is loaded, its associated DLL is not loaded, so the pointer to the method implementation is not set. These DLLs will not be loaded until the native method is called, which is achieved by calling java.system.loadLibrary().​

The final thing to note is that there is overhead in using local methods, and it loses many of the benefits of java. If we have no choice, we can choose to use local methods.

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

For more detailed articles related to Java Native Method, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn