Home > Java > javaTutorial > How to use Native modifier in Java

How to use Native modifier in Java

王林
Release: 2023-04-22 15:46:08
forward
1877 people have browsed it

Use of Native modifier

native is mainly used for methods

1. A native method is an interface for Java to call non-Java code. A native method means that the implementation of the method is implemented in a non-Java language, such as C or C.

2. When defining a native method, the implementation body is not provided (more like defining a Java Interface), because its implementation body is implemented externally by a non-Java language.

Explanation

The Java language itself cannot access and operate the bottom layer of the operating system, but it can call other languages ​​through the JNI interface to achieve access to the bottom layer.

JNI is Java Native Interface, a native programming interface, which is part of the Java Software Development Kit (SDK). JNI allows Java code to use code and code libraries written in other languages.

The Invocation API (part of JNI) can be used to embed the Java Virtual Machine (JVM) into native applications, allowing programmers to call Java code from within native code.

Example

In the JDK source code, take system.class as an example. Since the relevant methods of the operating system are called, and the operation is not implemented in Java, there will be many native modifications. Method

How to use Native modifier in Java

#What is the use of the Native modified method

I accidentally looked at the code today and found that others have written methods like this, and there are several dlls in the jar. The file seemed strange, so I opened the code and found the following writing method.

public native String GSMModemSMSReadAll(String s, int i);
public native String GSMModemGetErrorMsg(String s);
public native boolean GSMModemIsConn(String s);
Copy after login

I think it’s quite interesting. I searched for information on the Internet and found it good. Another method of using java to call other languages ​​(I didn’t know it before -_#)

I spent two hours today reading an English article about what Native Method is. The following is what I Based on the understanding of the original text.

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
    {undefined
      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 ;
    }
Copy after login

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 (just like java's multi-threading) before entering the implementation of this method.

A native method can return any java type. Includes non-basic types, and can also be controlled by exceptions. 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 the local method and can override this method in Java language (this seems a bit strange), the same if a local method is marked by fianl , it cannot be overridden after being inherited.

Local methods are very useful because they effectively extend the 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.

Why use Native Method

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

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.

Interaction with the operating system: The JVM supports the Java language itself and the runtime library. It is the platform on which Java programs rely on. It consists of an interpreter (interpreting bytecode) and Consists of libraries that connect to native 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.

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 back 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

The above is the detailed content of How to use Native modifier in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template