A Java how objects are created?

I. Introduction

When we work for some time, I will think of something to explore the underlying point, but the underlying research, we need a very strong basics, so Here's to ask the way to learn about class loading system in the JVM. In Java, how an object is being created?

Below this picture too familiar with it, just as it is to review what ~ ~ ~ ~
Here Insert Picture Description

Second, the class loader subsystem

The above memory structure diagram JVM describe in detail, this is obtained following detailed JVM memory structure of FIG. The following highlights look at the yellow part of theClass loader subsystem.
Here Insert Picture Description
In order to facilitate review and understanding, the system converts the yellow class loading into a lower portion of FIG above, should be able to read it.
Here Insert Picture Description

JavaAt newthe time an object will first see the object belongs to the class has not been loaded into memory, if not, it will first passThe fully qualified name of the classTo load. After the first load and initialize the class is completed, and then create work objects.

What is the fully qualified name of the class?
The fully qualified name of the class, the class name is the full name with the package path points separated by, for example: java.lang.String
unqualified name of the class, we usually say that the class name, a path without packet, for example: String .

Third, the class loading process (first time the use of such)

First time following start talking about a class, newan object can be divided into two processes:① load and initialize the class, ② create objects

Java It is to useParent delegation modelTo load the class, and that is what parents delegation model it?

A brief parent delegation model their work is:If a class loader (ClassLoader) received a request class is loaded, it first does not own to try to load this class, but to entrust the request to the parent class loader to complete each level of the class loader is true when, all of a load request eventually be transferred to the top of the boot class loader, only when the parent class loader feedback they can not complete the loading request (its search does not need to load the class to find), sub-loaded will only attempt to load their own.

Parents benefit delegation mechanism are: the ability to effectively ensure global uniqueness of a class, when the same class name appears more limited program, the class loader when performing loading, always will load only one of these classes.

具体可参考下面这篇文章:Java类加载器

1、加载并初始化类

(1)、加载(Loading)

由类加载器负责根据一个类的全限定名来读取此类的二进制字节流到JVM内部,并将这个字节流所代表的静态存储结构转化为方法区的运行时数据结构,然后在内存中生成一个代表这个类的java.lang.Class对象,作为方法区这个类的各种数据的访问入口。

(2)、验证(Verify)

验证目的在于确保Class文件的字节流中包含信息符合当前虚拟机要求,保证被加载类的正确性,不会危害虚拟机自身安全。验证主要包括:

格式验证:验证是否符合class文件规范

语义验证:检查一个被标记为final的类型是否包含子类;检查一个类中的final方法是否被子类进行重写;确保父类和子类之间没有不兼容的一些方法声明(比如方法签名相同,但方法的返回值不同)

操作验证:在操作数栈中的数据必须进行正确的操作,对常量池中的各种符号引用执行验证(通常在解析阶段执行,检查是否可以通过符号引用中描述的全限定名定位到指定类型上,以及类成员信息的访问修饰符是否允许访问等)

(3)、准备(Preparation)

①、为类变量分配内存并且设置该类变量的默认初始值,即零值;
②、这里不包含用final修饰的static,因为final在编译的时候就会分配了,准备阶段会显式初始化;
③、这里不会为实例变量分配初始化,类变量会分配在方法区,而实例变量是会随着对象一起分配到Java堆中;(由于还没有产生对象,实例变量不在此操作范围内)

(4)、解析(Resolution)

①、将常量池内的符号引用转换为直接引用的过程;
②、事实上,解析操作往往会伴随着JVM在执行完初始化之后再执行;
③、符号引用就是一组符号来描述所引用的目标。符号引用的字面量形式明确定义在《java虚拟机规范》的Class文件格式中。直接引用就是直接指向目标的指针、相对偏移量或一个间接定位到目标的句柄。
④、解析动作主要针对类或接口、字段、类方法、接口方法、方法类型等。对应常量池中的CONSTANT_Class_info、CONSTANT_Fieldref_info、 CONSTANT_Methodref_info等。

以上2、3、4三个阶段又合称为链接阶段,链接阶段要做的是将加载到JVM中的二进制字节流的类数据信息合并到JVM的运行时状态中。

(5)、初始化(Initialization)(先父后子)

先为静态变量赋值,再执行static代码块。
注意:static代码块只有JVM能够调用

因为子类存在对父类的依赖,所以类的加载顺序是先加载父类后加载子类,初始化也一样。不过,父类初始化时,子类静态变量的值也有的,是默认值。

最终,方法区会存储当前类的信息,包括类的静态变量、类初始化代码(定义静态变量时的赋值语句 和 静态初始化代码块)、实例变量定义、实例初始化代码(定义实例变量时的赋值语句实例代码块和构造方法)和实例方法,还有父类的类信息引用。

总结一下:初始化做了什么操作?

2、创建对象

加载并初始化类之后,我们就可以new一个对象了。那么,new一个对象,JVM又做了哪些事情呢?

(1)、在堆区分配对象需要的内存

分配的内存包括本类和父类的所有实例变量,但不包括任何静态变量,所有对象共享一个静态变量。

(2)、对所有实例变量赋默认值

(3)、执行实例初始化代码

初始化顺序是先初始化父类再初始化子类,初始化时先执行实例代码块然后是构造方法

(4), if there are similar Child c = new Child () in the form of c-quoted definition of type Child reference variable c, and assigning the address to which the object heap area in the stack area

Note that each subclass object holds a reference to the parent object, the parent object can be called by the super keyword in the interior, but is not accessible externally.

IV Summary

From the above analysis, we should know that creating a Java object, you need to perform the following procedures:
Here Insert Picture Description

Published 123 original articles · won praise 230 · views 190 000 +

Guess you like

Origin blog.csdn.net/zxd1435513775/article/details/101760124