In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will show you how to analyze the principle of java polymorphism from the Hotspot source code level. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.
How C++ achieves Polymorphism
The implementation of polymorphism, now almost all programming languages are based on virtual tables, English vtable. I didn't say all of them here, because I don't know all the languages, and I don't dare to say anything, so as not to be sprayed. ^ _ ^
Where is the virtual watch of C++? The header of the object created in new. What is stored in the virtual table? It's a virtual function. C++ this knowledge I will not say too much, a lot of friends do not understand Clippers, talk too much is not necessary, as a Java programmer, to this extent enough.
Because hotshot is mainly written in C++, talking about the virtual table of C++, you should be able to understand this picture.
Otherwise, my partner will always ask me why there is a virtual table at the C++ level for the C++ object corresponding to the class of Java. I don't see such a code anywhere.
After figuring out the virtual table, it is much easier to understand the distribution of the virtual table. Virtual table distribution, in fact, is to get the virtual table record through the virtual table memory address, and then find it in the virtual table through the function name + signature containing parameter information and return value information. Because it is looking from the back, if the subclass overrides the method of the parent class, the method of the subclass will be called.
The virtual form distribution of C++, I just briefly talked about it, and we have no idea if we talk too much. The virtual table distribution of JVM, which I will explain in more detail later. Many phenomena, if you do not understand its bottom, is not puzzled. There are so many. Why? Why? ^ _ ^
So although Java is good, the bottom is also very important. By the way, virtual tables are implemented in arrays, which are not as complicated as some buddies think.
Virtual tables in JVM
The virtual watch of JVM is not the same as that of C++. What is the difference? The study of virtual tables studies three things: where the virtual tables are, what the structure of the virtual tables is, and what is the distribution mechanism of the virtual tables. JVM's virtual table distribution, etc., JVM's virtual table is also implemented in an array, so this difference is reflected in where the virtual table is?
Java, and the corresponding C++ object in JVM is the klass model. The object of Java and the corresponding C++ object in JVM is the oop model. The virtual table in C++ is in the header of the object, while the virtual table of JVM is in the header of the klass model, that is, the header of the Java class object. This difference must be kept in mind so that you can understand the memory layout of Java objects.
Let me ask you a question: does a class we define randomly have a JVM virtual table? Actually, there is. What is the memory address of which methods? Before you answer this question, you have to figure out what kind of method will be stored in the virtual table. Only methods of public and protect types that are not modified by static or final can be called by polymorphism and enter the virtual table. Because all classes in Java are subclasses of Object, the methods in Object that meet this condition are in the virtual table of each class.
It's time for the little partner to be unconvinced. What's the matter? give evidence. I won't talk about how to check it. It's a little complicated. I don't have a certain skill for hotspot and I have no idea.
How to realize virtual table distribution by Java
Some friends do not understand: I can only Java work is no problem ah, why do I want to learn the bottom? Do you want to work with good people in a big factory? Do you want to be the boss in the eyes of others? Do you want to be famous in a certain field? All these need strength to support.
Some friends said, "what am I doing with a handwritten JVM?" Then I will use my handwritten JVM to explain this knowledge point. This is one of the reasons why you have a handwritten JVM.
JVM implements virtual table distribution, and there are two bytecode instructions: invokevirtual and invokeinterface. As soon as you look at the stack when executing invokeinterface, you should be able to understand.
Although the operands after invokeinterface are interface method information. But the real object will be passed as a this. So when calling, get the real object from the Operand stack, and then get the C++ class object corresponding to TestDuotai, that is, the klass model, through the type pointer in the object header. As mentioned earlier, the virtual table is in the head of this object. Then look for it in the virtual table through the function name + the signature containing parameter information and return value information. Because it is looking from the back, if the subclass overrides the method of the parent class, the method of the subclass will be called. This is the underlying principle of JVM virtual table distribution.
The above is how to analyze the full content of the principle of java polymorphism from the Hotspot source code level, more content related to how to analyze the principle of java polymorphism from the Hotspot source code level can search the previous article or browse the following article to learn ha! I believe the editor will add more knowledge to you. I hope you can support 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.