Java Native keyword---java downgrade in other languages

Source: Internet
Author: User

Today, when researching the Java base Class library, the object class found a keyword: native

Hey? What is this stuff? It knows me, I don't know it!

Hey, don't worry, Baidu a bit.

Java Native Keywords

I. What is native Method
In a nutshell, a native method is a Java interface that calls non-Java code. A native method is a Java approach: the implementation of this method is implemented by non-Java languages, such as C. This feature is not specific to Java, many other programming languages have this mechanism, such as in C + +, you can use the extern "C" to tell the C + + compiler to call a C function.
"A native method is a Java method whose implementation was provided by Non-java code."
When defining a native method, the implementation body is not provided (some like the definition of a Java interface) because its implementation is implemented outside the non-Java language. , an example is given below:

     PackageJava.lang;  Public classObject {... Public Final nativeClass<?>getclass ();  Public native inthashcode (); protected nativeObject Clone ()throwsclonenotsupportedexception;  Public Final native voidnotify ();  Public Final native voidNotifyall ();  Public Final native voidWaitLongTimeoutthrowsinterruptedexception; ......    }

The identifier native can be used with all other Java identifiers, except for abstract. This is justified because native implies that these methods are implemented, except that the implementations are non-Java, but the abstract clearly indicates that these methods are not implemented. Native and other Java identity Connect prompt time, its meaning and non-native method is not different.

A native method can return any Java type, including non-basic types, and can also be used for exception control. The implementation of these methods can make an exception and throw it, which is very similar to the Java approach.
The presence of the native method does not have any effect on other classes calling these local methods, and the other classes that actually 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.

If a class with a local method is inherited, the subclass inherits the local method and can rewrite the method in the Java language (this may seem strange), as well if a local method is fianl identified, it cannot be overridden after it is inherited.
Local methods are very useful, Because it effectively expands the JVM. In fact, the Java code we write has already used native methods, and in the implementation of the Concurrency (multithreading) mechanism of Sun's Java, many of the contact points of the operating system have been used locally, which allows the Java program to go beyond the Java runtime boundaries. With local methods, Java programs can do any application-level tasks.


two. Why use native Method
Java is very convenient to use, but some levels of tasks are not easy to implement in Java, or we are concerned about the efficiency of the program, the problem comes.
diplomacy with the Java environment:
sometimes Java applications need to interact with environments outside of Java. This is the main reason why the local method exists, and you can think about how Java needs to exchange information with some underlying systems, such as the operating system or some hardware. The local approach is one such communication mechanism: it provides us with a very concise interface, and we do not need to understand the tedious details outside of Java applications.
interacting with the operating system:
The JVM supports the Java language itself and the runtime library, which is the platform on which the Java program survives, consisting of an interpreter (the explanatory bytecode) and some libraries connected to the local code. However, after all, it is not a complete system, it often relies on some of the underlying (underneath) system support. These underlying systems are often powerful operating systems. By using the native method, we were able to implement the JRE's interaction with the underlying system in Java, and even some parts of the JVM were written in C, and if we were to use some of the Java language itself without providing the features of the encapsulated operating system, we would also need to use local methods.
Sun ' s Java
Sun's interpreter is implemented in C, which allows it to interact with the outside like some normal C. Most of the JRE is implemented in Java, and it 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 invocation of the Local Method SetPriority0 () in the class. This local method is implemented in C and is implanted inside the JVM, and on the Windows 95 platform, this local method will eventually invoke the Win32 setpriority () API. This is a specific implementation of a local method that is provided directly by the JVM, and more generally, the local method is provided by the external dynamic link library (external. dll) and then called by the JVM.


Three. How the JVM makes native method run:
We know that when a class is first used, the byte code of this class is loaded into memory and is only reloaded once. The entrance to the loaded bytecode maintains a list of all the method descriptors for that class, which contain information such as where the method code is stored, what parameters it has, the method's descriptor (public, and so on), and so on.
If there is a native in a method descriptor, this description converts sequential blocks will have a pointer to the implementation of the method. These implementations are within some DLL files, but they are loaded into the Java program's address space by the operating system. When a class with a local method is loaded, its associated DLL is not loaded, so pointers to the implementation of the method are not set. These DLLs are not loaded until the local method is called, which is implemented by calling Java.system.loadLibrary ().

The last thing to note is that the use of local methods is expensive, and it loses many of the benefits of Java. If there is no choice, we can choose to use local methods.

The native method can be likened to the Java program's interface to the C program, and its implementation steps:
1, declare the native () method in Java, and then compile;
2. Create an. h file with Javah;
3, write a. cpp file to implement the native export method, which needs to include the second step produced by the. h file (Note that it also contains the JDK jni.h file);
4, the third step of the. cpp file is compiled into a dynamic link library file;
5, in Java with the System.loadlibrary () method to load the fourth step generated by the dynamic link library file, the native () method can be accessed in Java.

The implementation method can be found on the Internet, this is not written

However, it leads to two things: javah.exe command and JNI


Scenarios where Java native methods are applicable
1. In order to use one of the features of the underlying host platform, this feature cannot be accessed through the Java API

2. In order to access an old system or use an existing library, this system or library is not written in Java

3. To speed up the performance of the program, a period of time-sensitive code is implemented as a local method.

I wrote an example for reference only:
First, write the Java file.

Package com.hode.hodeframework.modelupdate;

public class Checkfile
{
public native void Displayhelloworld ();

Static
{
System.loadlibrary ("test");
}

public static void Main (string[] args) {
New Checkfile (). Displayhelloworld ();
}
}
Then compile the class file according to the written file.
Then execute Javah-jni com.hode.hodeframework.modelupdate.CheckFile in the class root directory such as classes or bin,
You get a com_hode_hodeframework_modelupdate_checkfile.h file in the root directory.
Then write the com_hode_hodeframework_modelupdate_checkfile.c file based on the contents of the header file
#include "CheckFile.h"
#include
#include

Jniexport void Jnicall Java_com_hode_hodeframework_modelupdate_checkfile_displayhelloworld (JNIEnv *env, Jobject obj)
{
printf ("Hello world!/n");
Return
}
After compiling the build DLL file such as "Test.dll", the name matches the name in System.loadlibrary ("test")
VC Compilation Method: Cl-i%java_home%/include-i%java_home%/include/win32-ld com_hode_hodeframework_modelupdate_checkfile.c- Fetest.dll
At the end of the run, add the parameter-djava.library.path=[dll the path of the store]

Java Native keyword---java downgrade in other languages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.