Today, when studying the Java base class library and object class, we found a keyword: Native
Why? What is this? It knows me, but I don't know it!
Hey, it's okay, Baidu.
Java Native keywords
1. What is native method?
To put it simply, a native method is a Java interface that calls non-Java code. A native method is such a Java method: the implementation of this method is implemented by a non-Java language, such as C. This feature is not unique to Java. Many other programming languages use this mechanism, for example, in C ++, you can use extern "c" to notify 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 (for example, defining a Java Interface), because the implementation body is implemented outside of the non-Java language ., The following is an example:
Package java. Lang;
Public class object {
......
Public final native class <?> Getclass ();
Public native int hashcode ();
Protected native object clone () throws clonenotsupportedexception;
Public final native void Policy ();
Public final native void policyall ();
Public final native void wait (long timeout) throws interruptedexception;
......
}
The identifier native can be used with all other Java identifiers, except abstract. This is reasonable, because native implies that these methods have implementation bodies, but these implementations are non-Java, but abstract clearly specifies that these methods have no implementation bodies. When native is connected to other Java identifiers, its meaning is the same as that of non-native method.
A native method can return any Java type, including non-basic types, and can also perform exception Control. The implementation body of these methods can make an exception and throw it, which is very similar to the Java method.
The existence of native method does not affect the calling of these local methods by other classes. In fact, other classes that call these methods do not even know that they call a local method. JVM controls all details of calling local methods.
If a class containing a local method is inherited, The subclass will inherit this local method and can be rewritten in Java (this seems strange ), similarly, if a local method is identified by fianl, it cannot be overwritten after being inherited.
The local method is very useful because it effectively expands JVM. in fact, the Java code we have written has used local methods. In the implementation of Sun's Java concurrency (multithreading) mechanism, many contact points with the operating system use local methods, this allows Java programs to go beyond the boundaries of Java runtime. With the local method, Java programs can perform tasks at any application level.
Ii. Why NATIVE METHOD
Java is very convenient to use. However, it is not easy to implement some hierarchical tasks in Java, or we are very concerned about program efficiency.
Interaction outside the Java environment:
Sometimes a Java application needs to interact with an environment outside Java. This is the main reason for the existence of local methods. You can think about the situation that Java needs to exchange information with some underlying systems, such as the operating system or some hardware. The local method is such a communication mechanism: it provides us with a very simple interface, and we do not need to understand the tedious details outside of Java applications.
Interaction with the operating system:
JVM supports the Java language itself and the Runtime Library. It is a platform for survival of Java programs. It consists of an interpreter (interpreted bytecode) and some libraries connected to local code. However, after all, it is not a complete system. It often depends on the support of some underlying (underneath below) systems. These underlying systems are often powerful operating systems. By using the local method, we can use Java to implement the interaction between JRE and the underlying system, and even some parts of the JVM are written in C, if we want to use some java languages that do not provide encapsulated operating system features, we also need to use local methods.
Sun's Java
Sun's interpreter is implemented in C, which enables it to interact with external entities like some common C. Most of the JRE is implemented in Java, and it also interacts with the outside world through some local methods. For example, the setpriority () method of Java. Lang. thread is implemented in Java, but it calls the local method setpriority0 () in the class (). This local method is implemented in C and embedded in JVM. On the Windows 95 platform, this local method will eventually call the Win32 setpriority () API. This is the specific implementation of a local method directly provided by JVM. More often, the local method is provided by external dynamic link library and then called by JVM.
Iii. How does JVM run native method:
We know that when a class is used for the first time, the bytecode of this class will be loaded into the memory and will be loaded only once. At the entry of the loaded bytecode, a list of all the method descriptors of this class is maintained. These method descriptors contain information such as where the method code is stored and what parameters it has, method Descriptor (such as public.
If a method descriptor contains native, this descriptor block will have a pointer to the implementation of this 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 local method is loaded, the related DLL is not loaded, so the pointer to the method implementation is not set. These DLL files are loaded only when the local method is called, which is implemented by calling java. system. loadlibrary.
The last thing to note is that using a local method is overhead, and it loses many Java benefits. If you have no choice, you can use the local method.
The native method can be compared to the interface of a Java program and a C program. The implementation steps are as follows:
1. Declare the native () method in Java and compile it;
2. Use javah to generate a. h file;
3. Write a. cpp file to implement the native export method, which must contain the. h file generated in step 2 (note that it contains the JNI. h file in JDK );
4. Compile the. cpp file in step 3 into a dynamic link library file;
5. Use the system. loadlibrary () method in Java to load the dynamic link library file generated in Step 4. This Native () method can be accessed in Java.
The specific implementation method can be searched on the Internet. I will not write it here.
However, two more things are introduced: The javah.exe command and JNI
I wrote an example of calling the DLL of VB, But I haven't finished it yet. I won't write the part of the called function, but I only write the process of loading the DLL.
Java:
Public class testnativemothod {
Public static void main (string [] ARGs ){
// Process logic
}
Public native string callnativemothod ();
Static {
System. loadlibrary ("testnativemethodproj ");
}
}
VB part:
Option explicit
Public Function callnativemothod () as string
Callnativemothod = "this is a method implemented by VB"
End Function
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/jiakw_1981/archive/2008/10/14/3073613.aspx