Home  >  Article  >  Java  >  Detailed explanation of the problem of creating parent class objects in Java inheritance

Detailed explanation of the problem of creating parent class objects in Java inheritance

PHP中文网
PHP中文网Original
2017-06-20 10:18:431692browse

It is true that the parent class constructor is called, but the parent class object is not created at all, but the parent class constructor is called to initialize the properties.

It is really nonsense to say that calling the parent class constructor method is equivalent to creating a parent class object.
The new instruction opens up space, which is used to store various attribute references of objects, etc. If you decompile the bytecode, you will find that there is only one new instruction, so a space is opened, and one object is placed in each space.
Then, the subclass calls the properties, methods, etc. of the parent class, which is not an instantiated object.
In the bytecode, the subclass will have a u2 type parent class index, which belongs to the CONSTANT_Class_info type. CONSTANT_Utf8_info can be found through the description of CONSTANT_Class_info, and then the specified parent class can be found.
In your method, the attribute names are parsed on this, and then the actual variable content is stored in the space created by new. . .
The super keyword only accesses the data in a specific part of this space (that is, the memory part dedicated to storing parent class data). . . . . .

The default hashcode and equals (directly used == comparison) are the same, so this is basically in the same space, and there is no separate parent class object.


If a subclass can be forcibly converted into a parent class for use, it is because the Java virtual machine has the concepts of static type (appearance type) and actual type.
For example, Object t=new Point(2,3);
Then Object belongs to static type (appearance type) and Point belongs to actual type.
Both static type and actual type can change in the program. The difference is that the change of static type only occurs when using it, while the static type of the variable itself will not change, and the final static type is known during compilation. ;The change result of the actual variable type can only be determined during runtime. The compiler does not know what the actual type of the variable is when compiling


Memory of java objects The layout is determined by the class to which the object belongs. It can also be said that when a class is loaded into the virtual machine, the layout of the objects created by this class has already been determined.

Memory layout of java objects in Hotspot:
Each java object consists of an object header and an object body in memory.

The object header stores the meta-information of the object, including the reference to the class object Class to which the object belongs and some information about the hashcode and monitor.
The object body mainly stores the instance fields of the Java object itself and the instance fields inherited from the parent class, and the internal layout satisfies the following rules:
Rule 1: Any object is processed at a granularity of 8 bytes aligned.
Rule 2: Instance fields are arranged according to the following priority: long and double types; integer and floating point types; characters and short integer types; byte types and Boolean types, and finally reference types. These instance fields are aligned according to their respective units.
Rule 3: Instance fields in different class inheritance relationships cannot be mixed. First, the instance fields in the parent class are processed according to Rule 2, followed by the instance fields of the subclass.
Rule 4: If the distance between the last member of the parent class and the first member of the subclass is less than 4 bytes, it must be extended to the basic unit of 4 bytes.
Rule 5: If the first instance field of the subclass is a double or long integer, and the parent class has not used up 8 bytes, the JVM will break Rule 2, according to integer (int), short integer (short), byte type (byte), reference type (reference) order, filling the unfilled space.

The above are the rules for the memory layout of java objects.

Next, let’s talk about the instantiation method of java objects, which is the common method.
When we create a new object, the jvm has actually allocated the entire space of the object, and the instance domain layout of the entire object has been determined.
The instantiation method is to set the value of the object instance field to the corresponding space.

The method starts by calling the method of the parent class and ends with its own constructor method. The positional relationship between the declaration of the instance field and the instance initialization statement block will affect the bytecode order of the method generated by the compiler.

Let’s use an example to illustrate:
class Parent {
private short six;
private int age;
}

class Sub extend Parent{
private String name;
private int age;
private float price;
}

The memory layout of the current Sub object is as follows:
Super represents the so-called parent class storage space What exactly does that mean?
I think the super storage here is the green location!

The above is the detailed content of Detailed explanation of the problem of creating parent class objects in Java inheritance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn