Java local method

Source: Internet
Author: User

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:
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 how some non-Java code looks like in these 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 implementation bodies, but these implementations are non-Java, but abstract clearly specifies that these methods have no implementation bodies. When native is connected with other Java identifiers, its meaning is the same as that of non-native method. For example, native
Static indicates that this method can be called directly when no class instance is generated, which is very convenient. For example, if you want to use a native method to call a c class library. The third method above uses native synchronized. Before JVM enters the implementation body of this method, it will execute the synchronization lock mechanism (just like Java's multithreading .)
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 create an exception and throw it, which is very similar to the Java method. When a native method receives some non-basic types, such as an object or an integer array, this method can access these non-basic types, however, this native method depends on the implementation of the Java class you access. One thing to remember: we can
Access all Java features in the local implementation of method, but this depends on the implementation of the Java features you access, in addition, this is far less convenient and easy to use in Java.
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. Note that when we declare a local method as final. The method body implemented in Java may improve the efficiency during compilation due to inline. But a native
It is doubtful whether the final method can also obtain such benefits, but this is only a problem in code optimization and has no impact on function implementation.
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, it is not a complete system. It often relies 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 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.

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.