One . What is? Native Method
   Simply speak , One Native Method It's just one. java Call not java Code interface . One Native Method It's such a java Methods : The implementation of this method consists of java Language implementation , such as C. This feature is not java Unique to , Many other programming languages have this mechanism , For example C++ in , You can use it. extern "C" inform C++ The compiler calls a C Function of .
   "A native method is a Java method whose implementation is provided by non-java code."
   Defining a native method when , Does not provide an implementer ( It's like defining a java interface), Because its realization is caused by non java Language is realized on the outside ., An example is given below :   
    public class IHaveNatives
    {
      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 some non java The code is in these java What does it look like in the code (view).
    identifier native With all the others java Identifiers are used together , however abstract With the exception of . It's reasonable , because native Implying that these methods have implementers , It's just that these implementers are right and wrong java Of , however abstract But it clearly indicates that these methods have no implementers .native And others java When identifiers are used together , Its meaning is not Native Method There is no difference , such as native static Indicates that this method can be called directly without producing an instance of the class , It's very convenient , For example, when you want to use a native method To call a C Class library . The third method above uses native synchronized,JVM The synchronization lock mechanism is executed before entering the implementation body of this method ( It's like java The multithreading .)
    One native method Method can return any java type , Including non basic types , And it can also be used for exception control . The implementers of these methods can make an exception and throw it , This is related to java The method is very similar . When one native method When receiving some non basic types, such as Object Or an integer array , This method can access the interior of these non basic types , But it will make this native The method depends on the java The realization of the class . One thing to keep in mind : We can do it in one native method Access all of the java characteristic , But it depends on what you visit java Implementation of features , And it's not as good as java It's convenient and easy to use those features in a language .
    native method The existence of does not have any effect on other classes calling these local methods , In fact, the other classes that call these methods don't even know it's calling a local method .JVM Will control all the details of calling local methods . Note that when we declare a local method as final The situation of . use java When the implemented method body is compiled, the efficiency may be improved due to inlining . But one native final It is doubtful whether the method can also obtain such benefits , But it's just a matter of code optimization , There is no impact on function implementation .
    If a class containing local methods is inherited , The subclass inherits the local method and can use java Language rewrites this method ( This seems strange ), Similarly, if a local method is fianl identification , It can't be rewritten after it's inherited .
   Local methods are very useful , Because it effectively expands jvm. in fact , What we wrote java The code already uses local methods , stay sun Of java Concurrent ( Multithreading ) In the implementation of the mechanism , Many of the contact points with the operating system use local methods , This makes java Programs can go beyond java The boundaries of runtime . With local methods ,java Programs can do tasks at any application level .

Two . Why use Native Method
   java It is very convenient to use , However, some levels of tasks use java It's not easy to implement , Or when we care about the efficiency of the program , Here's the problem .
   And java Environmental diplomacy is mutual :
   Sometimes java Applications need to be associated with java The outside environment interacts . This is the main reason for the existence of local methods , You can think about java The situation in which information needs to be exchanged with some underlying system, such as the operating system or some hardware . The local approach is just such a communication mechanism : It provides us with a very simple interface , And we don't have to understand java Trivial details beyond the application .
   Interact with the operating system :
   JVM Supporting java The language itself and the runtime library , It is java The platform on which programs live , It has an interpreter ( Interpret bytecode ) And some libraries linked to local code . But anyway sample , It's not a complete system after all , It often depends on the bottom (underneath In the following ) System support . These underlying systems are often powerful operating systems . By using local methods , We can use java Realized jre Interaction with the underlying system , even to the extent that JVM Part of it is to use C Written , also , If we're going to use some java When the language itself does not provide encapsulated features of the operating system , We also need to use local methods .
    Sun's Java
    Sun The interpreter of C Realized , This makes it look like something ordinary C Interact with the outside as well .jre Mostly with java Realized , It also interacts with the outside world through some local methods . for example : class java.lang.Thread Of setPriority() The method is to use java Realized , But it calls the local methods in the class setPriority0(). This method is local C Realized , And implanted JVM Inside , stay Windows 95 On the platform , This local method will eventually call Win32 SetPriority() API. This is a concrete implementation of a local method by JVM Provide directly , More often than not, local methods are made up of external DLL (external dynamic link library) Provide , Then be JVM call .

3、 ... and .JVM How to make Native Method Run :
    We know , When a class is used for the first time , The bytecode of this class will be loaded into memory , And it will only load back once . At the entry of the loaded bytecode is maintained a list, These method descriptors contain such information : Where does the method code exist , What parameters does it have , Method descriptor (public And so on ) wait .
    If a method descriptor contains native, The descriptor block will have a pointer to the implementation of the method . These are implemented in some DLL In the file , But they will be loaded by the operating system into java The address space of the program . When a class with a local method is loaded , Its related DLL Not loaded , So the pointer to the method implementation is not set . Before the local method is called , these DLL Will be loaded , This is done by calling java.system.loadLibrary() Realized .
  
   The last thing you need to know is , There is an overhead in using local methods , It lost java A lot of the benefits of . If there's no choice , We can choose to use local methods .

Reference resources :http://blog.csdn.net/lansine2005/article/details/5753741

JAVA Details of local methods , What is? JAVA Local method ? More articles about

  1. Java Detailed explanation of interview questions :java Keywords in

    One ,final1. By final Decorated classes cannot be inherited 2. By final The decorated method cannot be overridden 3. By final Modified variables cannot be changed   The point is the third sentence . By final Modified variables cannot be changed , What can't be changed ...

  2. $.ajax() Methods, jquery Medium ajax Method

    jquery Medium ajax Method parameters are always forgotten , Here is a record of . 1.url: Requirements for String Parameters of type ,( The default is the current page address ) Address to send request . 2.type: Requirements for String Parameters of type , Request mode (p ...

  3. Java Improve ——equals() And hashCode() Methods,

    java.lang.Object There are two very important methods in the class : 1 2 public boolean equals(Object obj) public int hashCode() Object Class is the successor of class ...

  4. Use Java How to operate a text file

    Use Java How to operate a text file Abstract : first java Text file processing is not supported , In order to make up for this defect, we introduced Reader and Writer Two classes first java Text file processing is not supported , In order to make up for this defect ...

  5. Java Detailed explanation of memory structure

    Java Detailed explanation of memory structure Java Divide memory into : Stack memory , Heap memory , Method area , Local method area and register, etc . Next, we will introduce stack memory , Heap memory , The method area has its own characteristics : 1. Stack memory (1) Some basic types of variables and object reference variables are in functions ...

  6. Java volatile Keyword details

    Java volatile Keyword details volatile yes java One of the keywords in , Used to decorate variables . The variable modified by this key can prevent the instruction of this variable from being rearranged , And keep memory visible . In short, its function is : Prohibition refers to ...

  7. $.ajax() Methods, jquery

    $.ajax() Methods,   jquery Medium ajax Method parameters are always forgotten , Here is a record of . 1.url:  Requirements for String Parameters of type ,( The default is the current page address ) Address to send request . 2.type:  Requirements for Str ...

  8. jQuery in $.ajax() Methods,

    $.ajax() Methods, jquery Medium ajax Method parameters are always forgotten , Here is a record of . 1.url: Requirements for String Parameters of type ,( The default is the current page address ) Address to send request . 2.type: Requirements for Strin ...

  9. $.ajax() Methods, ajax And async attribute 【 original 】 Detailed case analysis —— Talking about Redis Common use of cache 5 Ways of planting (String,Hash,List,set,SetSorted )

    $.ajax() Methods,   jquery Medium ajax Method parameters are always forgotten , Here is a record of . 1.url:  Requirements for String Parameters of type ,( The default is the current page address ) Address to send request . 2.type:  Requirements for Str ...

  10. jQuery - Ajax ajax Methods,

    $.ajax() Methods, jquery Medium ajax Method parameters are always forgotten , Here is a record of . 1.url: Requirements for String Parameters of type ,( The default is the current page address ) Address to send request . 2.type: Requirements for Strin ...

Random recommendation

  1. java High tech - operation javaBean

    1. Yes javaBean Simple introspective operation of public class IntroSpectorTest { public static void main(String[] args) throws Ex ...

  2. cmd The working directory of the running program

    As shown in the figure ,cmd By entering the actual path of your own program , Or put the program in an environment variable and then in cmd In the implementation of , use start perform , The working directory of the program is cmd Current directory : stay cmd Enter a shortcut to the program in to execute the program , ...

  3. buildroot add to ssh, And the use of stftp service

    The last article about buildroot Basic operation , This chapter starts at once SSH Services and configuration sftp service , And static IP Set up . To configure : make menuconfig Target packages  ---& ...

  4. AngularJS-- Learning notes ( One )

    AngularJS The official website provides a sample project for learning :PhoneCat. This is a Web application , Users can browse some Android mobile phone , Learn more about them , And search and sort . about PhoneCat Project ...

  5. RTP Protocol analysis

      Catalog (?)[-] The first 1 Chapter      RTP summary RTP What is it? RTP Application environment Relevant concepts Streaming media The first 2 Chapter      RTP Detailed explanation RTP The protocol hierarchy of The sublayer of the transport layer Part of the application layer RTP Encapsulation RTCP Of ...

  6. .net Software Engineer interview questions ( Refer to the answer )

    One . Completion ( Every empty 1 branch , common 12 branch ) 1 Object oriented languages have __ encapsulation ______ sex .__ Inherit _______ sex .__ polymorphic ______ sex . 2 It works foreach Traversing the accessed object needs to be implemented ____Ienumerab ...

  7. Cocos2d-x 3.0final Terminator series tutorial 14-L new abel-Cocos2d-x Document

    Objective record New text tag class Label Other text tags Introduction to the use of font making tools Summary https://github.com/chukong/cocos-docs/blob/master/manual/framew ...

  8. ResolveUrl in ASP.NET - The Perfect Solution

    original text :ResolveUrl in ASP.NET - The Perfect Solution If you are looking for ResolveUrl outside of Page/Co ...

  9. hdu_5890_Eighty seven(bitset Optimize DP)

    Topic link :hdu_5890_Eighty seven The question : 50 Number ,10W A asked , Delete the second paragraph every time you ask i,j,k After the count , Is there an alternative 10 The sum of the numbers is 87 The plan , Just output ’Yes’ perhaps ’No’ Answer key : ...

  10. Matplotlib The problem of Chinese display

    Original address :http://blog.csdn.net/rumswell/article/details/6544377 #Matplotlib There is something wrong with the Chinese display , Of course, you can modify the configuration file matplotlibrc ...