Hi, I’m Mr. Doug Tooth.

Recently, when IMPLEMENTING OOP encapsulation mechanisms on the JVM, a question caused me to frown. The following code

public class Test_2 {
    public static void main(String[] args) {
        Test_2 obj = newTest_2(); }}Copy the code

So I distilled my wincing question into an interview question: How many objects does this object creation code create inside the JVM?

tetragonal

Two: a Test_2 Object and an Object.

Not to mention the ability to create Test_2 objects. Why was an Object created? Because all classes in Java inherit from the Object class, all Object creation calls class constructors, and Object constructors are called in these constructors. As shown in figure

This diagram is the bytecode instruction corresponding to the Test_2 constructor. If you don’t know bytecode, it’s very important. That’s what the JVM’s hack technology is for!

Logical and unassailable, right? How many small partners also think so message area lift a claw.

How do I prove that two objects were created? Don’t know!

anti-side

One: only Test_2 objects are created.

So why call the Object constructor? The opposition heart began to panic ~

If the Object class has attributes, how do I access attributes in the Object class without creating an Object Object? I support the pro view.

public class Object {
    private String author = "ziya";
}
Copy the code

What are you doing here? The rape!

Positive solution

Why did I define creating two objects as positive? Oh, shame, I thought so too. So, the answer is that the opposing view is correct. So what about those two questions? Oh, hold still and listen to me.

What is the purpose of calling the parent constructor? Before explaining this, let’s say what constructors do: they do assignment of non-static properties. Therefore, the constructor of the parent class is called to complete the assignment of non-static attributes in the parent class. That’s the first question.

Then the second question: How do I access the properties of a parent class without creating a parent object? The essence of the question is: where are the attributes in the parent class stored? In fact, the attributes of the parent class are stored in the subclass object, so there is no need to create a parent object! Is there someone who doesn’t like it? It’s okay, above

You can also view JVM memory via HSDB, and the results are the same

One more question: how are attributes of a parent class stored on a subclass object? Because the superclass object is called as a subclass object, the nonstatic attribute assignment statements in the superclass constructor apply to the subclass object.

How do you prove it? Those of you who have followed me to learn about JVM should know. Use the debugging function of Idea. As shown in figure

Why are there two different views? I thought about it later: most people equate calling constructors with creating objects. In fact, from the essence of a function: a function simply receives external arguments, performs related logic, and passes parameters. Constructors are essentially functions and do not have the ability to create objects.

I have a saying

Promise me, if you learn, don’t embarrass others with this problem, okay? If you can’t help it, don’t tell anyone you learned it from Teacher Ziya. I don’t want to bear the senseless hatred: NND, what crazy interviewer thought of this interview question! With the same program ape, what is too urgent!

If you are reading this article, please let me know if you have encountered this question in an interview somewhere. I’ll make a few more denunciations! I am different from sin!

digression

The child tooth teacher’s handwriting JVM small class is about to start recruiting students! What language? Java! Right! Write a Java virtual machine in Java. This virtual machine, if you will, supports all the functionality of the current Java architecture. For example, to operate the operating System, I use the System class of Java System; Like strings and arrays, I’m also using Java. Of course, you can do it yourself if you want.

I had written a version in C before, and rewrote it in C++ for teaching purposes, but the bar was still high. So it took some time to write another version in Java. Not all features of the JVM, but at this point you will be able to do your own Hotspot source research. Java implementation, for friends, the language should be no barrier.

Some of these functions still need to use C++ to achieve, because I open this small class goal in addition to let everyone be able to write a Java virtual machine by hand, but also ability to study Hotspot source code and he uses C language, C++ to write middleware source code. You can write something in C++, and C++ is on its way.

In order to ensure the quality of teaching, only 30 students can be enrolled in the first phase, free of charge. About 22 classes + 50 classes. 1, using Java to write a Java virtual machine; 2. Able to write in C language and C++; 3. Have the ability to study Hotspot source code and middleware source code written by him in C language and C++.

Jvm-ziya: JVM-ziya: JVM-ziya: JVM-ziya: JVM-Ziya: JVM-Ziya