In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use the Native modifier in Java. It is very detailed and has a certain reference value. Friends who are interested must finish it!
The use of the Native modifier native is mainly used in methods
1. A native method is an interface for Java to call non-Java code. A native method means that the implementation of this 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 the actual body is implemented outside by a non-Java language.
Description
Java language itself can not access and operate on the bottom of the operating system, but can call other languages through the JNI interface to achieve access to the underlying layer.
JNI is the Java native interface (Java Native Interface), a native programming interface, and is part of the Java Software Development Kit (Java Software Development Kit,SDK). JNI allows Java code to use code and code bases written in other languages.
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.
Give an example
In the JDK source code, take system.class as an example, because the relevant methods of the operating system are called, and the operation is not implemented with Java, there will be a lot of native modification methods.
What is the use of Native modification?
Today, I accidentally looked at the code and found that someone else had a way to write it in this way, and there were several dll files in jar, which was strange, so I opened the code and found that it was written as follows.
Public native String GSMModemSMSReadAll (String s, int I); public native String GSMModemGetErrorMsg (String s); public native boolean GSMModemIsConn (String s)
I thought it was interesting. I looked for the information on the Internet, and it felt good. Another method to call another language using java (I didn't know-_ # before)
Today I spent two hours reading an English article about what Native Method is. Here is my understanding based on the original text.
What is Native Method?
In a nutshell, a Native Method is an interface for java to call non-java code. A Native Method is a java method that is implemented in a non-java language, such as C. This feature is not unique to java. Many other programming languages have this mechanism. In C++, for example, you can use extern "C" to tell 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 (somewhat like defining a java interface), because the actual body is actually implemented outside by a non-java language. 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;}
The declaration of these methods describes what some non-java code looks like in this java code (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, except that they are non-java, but abstract clearly indicates that these methods have no implementations. When native is used with other java identifiers, its meaning is no different from that of non-Native Method. For example, native static indicates that this method can be called directly when there is no instance of the class, which is very convenient, such as when you want to use a native method to call a C class library. The third method above uses native synchronized,JVM to execute a synchronous locking mechanism (like java's multithreading) before entering the implementation body of this method. )
A native method method can return any java type, including non-primitive types, and can also perform exception control. The implementation of these methods can make an exception and throw it, which is very similar to the method of java. When a native method receives some non-primitive type such as Object or an integer array, this method can access the interior of the non-primitive type, 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 native method, but it depends on the implementation of the java features you access, and it's not nearly as convenient and easy to use as those features in the Java language.
The existence of native method does not have any effect on other classes calling these local methods; in fact, other classes that call these methods do not even know that it is calling a local method. The JVM controls all the details of calling the local method. Note that when we declare a local method as final. Method bodies implemented in java may increase efficiency because of inlining when compiled. However, it is doubtful whether a native final method can get the same benefit, but it is only a matter of code optimization and has no impact on the functional implementation.
If a class that contains a local method is inherited, the subclass inherits the local method and can rewrite the party in the Java language (which may seem strange). Similarly, if a local method is identified by fianl, it cannot be overridden after it is inherited.
The local method is very useful because it effectively extends jvm. In fact, the java code we write has already used native methods, and in the implementation of sun's java concurrency (multithreading) mechanism, many points of contact with the operating system use native methods, which enables java programs to go beyond the boundaries of the java runtime. With local methods, java programs can do tasks at any application level.
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 arises.
Diplomatic interaction with the java environment: sometimes java applications need to interact with the environment outside the java. This is the main reason for the existence of local methods, and you can think about what happens when java needs to exchange information with some underlying system, such as the operating system or some hardware. The local approach is such a communication mechanism: it provides us with a very concise interface, and we don't need to know the tedious details outside of java applications.
Interacting with the operating system: JVM supports the java language itself and the runtime library, which is the platform on which java programs live, and consists of an interpreter (interpreting bytecode) and libraries connected to native code. However, it is not a complete system after all, and it often depends on the support of some underlying systems (underneath below). These underlying systems are often powerful operating systems. By using native methods, we are able to use java to interact with the underlying system of jre, and even some parts of JVM are written in C, and if we want to use some features of the operating system that are not encapsulated by the java language itself, 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 native methods. For example, the setPriority () method of the class java.lang.Thread is implemented in java, but its implementation calls the local method setPriority0 () in the class. This native method is implemented in C and is embedded inside JVM, and on Windows 95 platforms, this native method will eventually call Win32 SetPriority () API. This is a concrete implementation of a local method provided directly by JVM, and more often provided by an external dynamic link library (external dynamic link library) and then called by JVM.
How does JVM make Native Method run?
We know that when a class is used for the first time, the class's bytecode is loaded into memory and is only loaded once. A list of all method descriptors of the class is maintained at the entry of the loaded bytecode, which contains information about where the method code is stored, what parameters it has, the method descriptor (public, etc.), and so on.
If a method descriptor contains native, the descriptor block will have a pointer to the implementation of the method. These implementations are in some DLL files, but they are loaded into the address space of the java program by the operating system. When a class with a local method is loaded, its associated DLL is not loaded, so the pointer to the method implementation is not set. These DLL are not loaded until the local method is called, which is achieved by calling java.system.loadLibrary ().
Finally, it is important to note that there is overhead in using the local method, which loses many of the benefits of java. If we have no choice, we can choose to use the local method
The above is all the content of the article "how to use Native modifiers in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.