In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to understand the distribution mechanism of common knowledge points in Java? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
In Java, the methods that meet the requirements of "know at compile time, immutable at run time" are mainly static methods and private methods. Neither of these methods can be overridden by inheritance or other methods, so they are suitable for parsing when the class is loaded.
There are four method invocation instructions in the Java virtual machine:
Invokestatic: calls static methods.
Invokespecial: invokes instance constructor methods, private methods, and super.
Invokeinterface: call the interface method.
Invokevirtual: call methods (virtual methods) that cannot be called by the above instructions.
Any method that can be called by invokestatic and invokespecial instructions can determine the unique version of the call during the parsing phase. Those that meet this condition are static methods, private methods, instance constructors, and parent methods that resolve symbolic references to direct references to the method when the class is loaded. These methods are called non-virtual methods, while other methods are called virtual methods (except the final method).
Although the final method is called using the invokevirtual instruction, it is a non-virtual method because it cannot be overridden and the selection of polymorphism is unique.
one。 Static dispatch
Access to class fields is also statically dispatched.
People man = new Man ()
Static dispatch is mainly aimed at overloading, how to choose when the method is called. In the above code, People is called the reference type of a variable, and Man is called the actual type of the variable. Static types are known at compile time, while dynamic types are known at run time, and the compiler cannot know what the actual type of a variable is.
When overloaded, the compiler determines by the static type of the parameter rather than the actual type. And static types are known at compile time, so the compiler selects methods based on the static types of overloaded parameters.
In some cases, there are multiple overloads, so how does the compiler choose? The compiler will choose the "most appropriate" version of the function, so how to determine the "most appropriate"? The closer you get to the type of the incoming parameter, the easier it is to be called.
two。 Dynamic dispatch
Dynamic dispatch is mainly aimed at rewriting and is called using the invokevirtual instruction. Invokevirtual instruction polymorphism lookup process:
Find the actual type of object that the first element at the top of the Operand stack points to, and write it down as C.
If a method is found in type C that matches the simple name described in the constant, the access permission is checked. If it is passed, a direct reference to the method is returned, and the search process ends; if the permission verification fails, a java.lang.IllegalAccessError exception is returned.
Otherwise, search and verify each parent class of C from the bottom to the previous step 2 according to the inheritance relationship.
If the appropriate method is never found, a java.lang.AbstractMethodError exception is thrown.
three。 Implementation of virtual mobile dispatch
Because dynamic dispatch is a very tedious action, and the method version selection of dynamic dispatch needs to consider searching the appropriate target method in the method metadata of the class at run time, a virtual method table (invokeinterface has interface method table) is established in the method area to improve the performance in the implementation of the virtual machine.
The virtual method table stores the actual entry address of each method. If a method is not overridden in the subclass, the entry in the subclass's virtual method table is the same as the parent entry, and if the subclass overrides this method, the address in the subclass method table is replaced with the entry address of the subclass implementation version.
This is the answer to the question on how to understand the distribution mechanism in the common knowledge points of Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.