Hi, I’m Mr. Doug Tooth.
This article is a follow-up to the previous article on how JVM polymorphism is implemented. If you haven’t seen it yet, I suggest you do. portal
In my last post, I gave you a passing grade and a 70-80 score on this interview question, but today I’m going to tell you what to do if you want to get close to a perfect score. Because will design to the C++ virtual table and C++ polymorphic implementation, how this you do not understand or not familiar with, the interview suggested not to come out, lest encounter understand C++ to you to a serial call you asked meng. I have a video tutorial planned for C++ later. If you are interested in the C++ course of teacher ziya, you can pay attention to the public account: hard nuclear teeth, which will be pushed to you in this public account. In fact, not only C++, C language, assembly, are in my plan.
Here is a knowledge point for you. I said in yesterday’s post that it was so damn cool to have polymorphic people in mind when DESIGNING OOP mechanisms. Let me tell you why I said that. Or why OOP is encapsulation, inheritance, and polymorphism. For decades, we haven’t added, subtracted or changed one.
With the need of polymorphic achieved through dynamic binding, binding popular speak be let different object to call the same function, or the other way round, is to make the same function with different object binding, so the polymorphic implemented a major premise is, must be an object-oriented programming language. At the same time, functions and objects are bound to each other, meaning that functions are also part of the object, which has the nature of encapsulation. Because of encapsulation, there are objects. At the same time, a function can bind multiple objects, which means that different objects have the same behavior, which is the meaning of inheritance. Therefore, one of the three characteristics of object orientation is indispensable. Encapsulation and inheritance are actually prepared for polymorphism, or, encapsulation and inheritance make polymorphism, and polymorphism maximizes the meaning of encapsulation and inheritance.
How does C++ implement polymorphism
Polymorphic realization, now almost all programming languages are based on virtual table implementation, English Vtable. Here I did not say all, because I also do not understand all the language ha, dare not say, lest be sprayed. ^_^
Where are the virtual tables in C++? The header of the object created in new. What is stored in the virtual table? Is a virtual function. I will not talk too much about the knowledge of C++, many partners do not know C++, there is no need to talk more, as a Java programmer, understand this level is enough.
Since hotshot is mainly written in C++, it shows virtual tables in C++, so this should make sense to you.
Otherwise, some friends always ask me: why does the C++ object corresponding to the Java class have C++ level virtual table? I don’t see any such code anywhere.
Once you understand virtual tables, it’s much easier to understand virtual table distribution. Virtual table distribution, in fact, is to obtain the virtual table record through the virtual table memory address, and then search the virtual table through the function name + the signature containing parameter information and return value information. Because we’re looking backwards, if a subclass overrides a parent class’s method, the subclass’s method is called. C++ virtual table distribution, I just briefly talk about the next, we don’t have a concept. Virtual table distribution for the JVM, which I’ll talk about in more detail in a moment. A lot of phenomena, if you do not understand its bottom, is not puzzled. There are so many why? Why is that? ^_^
So as good as Java is, the underlying layer is also important. By the way, virtual tables are implemented using arrays, not as complicated as some people think.
Virtual tables in the JVM
JVM virtual tables are not quite the same as C++ virtual tables. What are the differences? Studying virtual tables studies three things: where virtual tables are, what structure virtual tables are implemented with, and what the distribution mechanism for virtual tables is. JVM virtual table distribution, etc., JVM virtual table is also implemented with arrays, so this difference is reflected in the virtual table.
Java classes, and the corresponding C++ object in the JVM is the klass model. Java objects, the corresponding C++ objects in the JVM are oop models. The virtual table in C++ is in the object header, whereas the virtual table in the JVM is in the header of the klass model, which is the header of Java class objects. This distinction must be kept in mind so that you can understand the memory layout of Java objects.
Question: does a random class we define have a JVM virtual table? Well, there is. So what’s the memory address of that method? To answer this question, we need to understand what methods are stored in the virtual table. Only public and protect methods that are not static or final can be called polymorphically and enter the virtual table. Because all classes in Java are subclasses of Object, methods in Object that satisfy this condition are in the virtual table of each class.
It’s time for the unconvinced partner. What’s the matter? Show me the evidence. I won’t talk about how to check it, it’s a little complicated. Hotspot does not have a certain ability to speak also have no concept.
How does Java implement virtual table distribution
Some friends do not understand: I only know Java work no problem, why should I learn the bottom? Do you want to work with great people in a big factory? Do you want to be seen as a big shot? Would you like to be famous in a certain field… These need strength to support.
Some friends say: Why should I write a JVM by hand? I’m going to use my hand-written JVM to explain this. That’s one of the implications of having a handwritten JVM.
The JVM implements virtual table distribution with two bytecode instructions: Invokevirtual and InvokeInterface. In the last article we looked at InvokeInterface in depth, and in this article we’ll continue with this instruction. Let’s take a look at how JVMS are distributed. You can see this by looking at the stack when invokeInterface is executed.
Although the operand after invokeInterface is the interface method information. But the real object will be passed as this. So when called, the real object is retrieved from the operand stack, and the corresponding C++ class object, the klass model, is retrieved from the type pointer in the object header for TestDuotai. As I said, the virtual table is at the head of this object. Then use the function name + the signature containing parameter information and return value information to find the virtual table. Because we’re looking backwards, if a subclass overrides a parent class’s method, the subclass’s method is called. This is the underlying principle behind virtual table distribution for the JVM. This one is a little hard to understand, and it may require a deeper foundation. I don’t know if I can read it like this. If you can’t read it like this, you can add my wechat (JVM-Ziya) to ask me
A series of recommendations
1. Super fast positioning OOM package plan
2. Contribute a super routine JVM interview question