JAVA native native method


1. What is the Native Method
   Simply put, a Native Method is a non-call interface java java code. A Native Method is a method java: implementing the method is implemented by a non java language, such as C. This feature is not unique to java, many other programming languages have this mechanism, such as in C ++, you can use extern "C" inform the C ++ compiler to call a C function of.
   "A native method is a Java method whose implementation is provided by non-java code."
   When defining a native method, it does not provide implementations body (somewhat like the definition of a java interface), because it is implemented in a non-member java language out to achieve. The following gives an example:   
    public class IHaveNatives
    {
      Native public void Native1 (int X);
      Native static public Long Native2 ();
      Native the synchronized Private a float Native3 (Object O);
      Native void Native4 (int [] ary) throws Exception;
    }
    statement describes some of these methods in these non-java code java code what looks like (view).
    Native identifier may be used in conjunction with all other identifiers java, except abstract. This is reasonable, because the native suggesting that these methods are to achieve the body, but these implementations is a non-java, but the abstract is clearly specified None of these methods to achieve body. When native identifiers used in conjunction with other java, its meaning with non-Native Method and non-discriminatory, such as native static show that this method can be called directly when no instance of the class, which is very convenient, such as when you want to use a native method to call when a C library. The third method above uses native synchronized, JVM before entering the implementation of this method performs synchronous body lock mechanism (like the multithreaded java.)
    A native method java method can return any type, including non-basic types, and The same can be exceptional control. These methods may be implemented body made and to throw an exception, and this is very similar to the method java. When a non-native Upon receiving the basic types such as Object Method or an integer array, the method of this non-access these basic interior, but this will depend on the implementation method of the native java classes you visit. One thing to keep in mind: we can access all the java properties in the local implementation of a native method, but it depends on the characteristics of java realize you visit, and doing so is far less than those in use java language easy and convenient features.
    the presence of native method calls the other classes will not have any impact on these local methods, in fact, call these methods of other classes do not even know what it is calling a local method. JVM control all the details of the native method call. Note that when we declare a native method for the final of the situation. Java implementation method thereof are likely to be produced inline improve the efficiency is compiled. But whether a native final method can also obtain such benefits is questionable, but it is only a matter of code optimization, there is no effect on the function implementation.
    If a class contains native method is inherited, the subclass inherits the native method java language and can override this method (this seems to look a little odd), if the same method is fianl a local identity, it can not be inherited It is rewritten.
   Local method is very useful because it effectively extends the jvm. In fact, the code we write java have used local methods, mechanisms to achieve in the sun java concurrency (multithreading), many of the operating system point of contact We have used local methods, which makes it possible to go beyond the boundaries of java program java runtime. With local method, java program can make any application level tasks.


II. Why use Native Method
   when java is easy to use, but some level tasks using java is not easy, or we are very concerned about the efficiency of the program, the problem came.
   Interaction with the external environment java:
   sometimes java application needs to interact with the environment outside of java. This is the main reason for the existence of local methods, you may need to think about the situation at the time of java with some of the underlying system hardware such as operating system or some exchange of information. Local exchange method is such a mechanism: it provides a very simple interface for us, and we do not need to understand the complicated details beyond the java application.
   Interact with the operating system:
   the JVM and runtime library supports java language itself, it is a java program for the survival of the platform, which consists of an interpreter (bytecode interpretation) and some connections to native code libraries. In any case, however, it is after all not a complete system, which often rely on some of the underlying (underneath below) support system. These systems are often strong underlying operating system. By using native methods, we were able to achieve with java jre interaction with the underlying system, and even some parts of the JVM is written in C, and, if we want to use some java language itself does not provide the operating system package features we also need to use native methods.
    The Java's Sun
    Sun interpreter is implemented in C, which makes it like some ordinary C as interaction with the outside. Most jre is the realization of java, it also adopted a number of local methods to interact with the outside world. For example: class java.lang.Thread of setPriority () method is the realization of java, but it is the type of implementation calls in the local method setPriority0 (). This local approach is implemented in C, and is implanted inside the JVM, on the Windows 95 platform, the native methods will eventually call Win32 SetPriority () API. This is a concrete realization of a native method provided directly by the JVM, the situation is more local methods provided by an external dynamic link libraries (external dynamic link library), and then call the JVM.


How three .JVM run up the Native Method:
    We know that when a class is first to use the bytecode of the class will be loaded into memory, and will only return once the carrier. Maintained at this inlet is loaded with a byte code class descriptors List all methods, these methods descriptor contains messages such as: a method where the code is stored in, what parameters it has, the method descriptor (public of class) and so on.
    If there is a native method descriptor, the descriptor block will have a pointer pointing to realize the method. These implementations in a number of DLL files, but they will be loaded into the address space of the operating system java program. When a class is loaded with the local-method, which is not related to the DLL is loaded, a pointer so implemented method will not be set. Before the local method is called, the DLL will be loaded, this is accomplished by calling java.system.loadLibrary ().
  
   Finally, it should be reminded that, using the local approach is overhead, it lost a lot of benefits of java. If you have no choice, we can choose to use native methods.
Published 20 original articles · won praise 2 · views 40000 +

Guess you like

Origin blog.csdn.net/jji8877032/article/details/48857773