Overview of Java code execution

Java code through three stages: Stage Source (Source) -> class loading stage (ClassLoader) -> runtime phase (Runtime)


      

First, let's clarify what Java code for the entire implementation process, let them have a full understanding of:

Java source (.java) via Java compiler later (the javac), generate one or more bytecode (.class) file, the JVM will be performed every byte code by a class loader ClassLoader loaded into memory, then by bytecode verifier checksum , J AVA interpreter translates them into the machine code , the operating system running on the last explanation.

 

When you want to use a class , if the class has not been loaded into memory, the system will be via load connections, initialize three steps to achieve this class is initialized:

Load is the class file is read into memory, and whom to create a Class object (any class system will be created and create a Class object only when used)

  JVM conduct class loading stage requires the following three things:    

    1. a class to get the definition of a binary byte stream class fully qualified name 

    2. The byte stream represents static storage structure into a run-time data structure area method

    3. In the java stack generates a representative of this class java.lang.Class objects, a method area to access the data entry

  The final product is loaded class located heap Class object region, encapsulates the Class object class data structure in the process zone, and provides the interface access method for a data structure of the area Java programmers

 

  Type of loading timing

    1. Create an instance of the class

    2. Use the class static variable or static variable assignment

    3. Call the static method of class

    4. Use of a reflection mode to force the creation of a class or interface objects corresponding java.lang.Class

    The subclass of a class initialization

    6. direct use of java command to run a master class

  Popular to say that as long as the use of things like class will load

 

  JVM will produce three runtime class loader to load the composition hierarchy initialization

  • Bootstrap ClassLoader root class loader 

          Written in C ++

          Also known as the bootstrap class loader is responsible for loading the java core class loader can not be obtained directly

          Such as System, String, etc., in the JDK JRE lib directory rt, jar file

  • Extension ClassLoader extension classloader

          jar package into library work under the charge of JRE extension directory in the jar package loading jre / lib / jar or bag -Djava, ext, dirs specified directory under the ext directory

  • System ClassLoader system class loader (load write your own class and third-party libraries (imported jar package))

          Responsible for the JVM loads class files from the java command, and classpath environment variable specified jar package and class path when you start

 

 

Connection is to merge into binary data class JRE in 

  Even then divided into the following three steps:

    Verify  checks the correctness of the data file loaded Class

      • File format test: test for compliance with Class byte stream file format, and can be the current version of virtual machine processing
      • Metadata test: bytecode information described semantic analysis, to ensure compliance with the content of its description of the Java language specification requires
      • Bytecode verifier: the data flow and control flow analysis to determine the semantics of the program is legitimate, logical
      • Symbol reference test: test reference symbols can be seen as information other than the type itself (constant pool reference symbols) matching check is performed

      Has the correct internal structure (constructors, methods, variables, code blocks), and other types of coherence and

    Prepare  the stage formally allocate memory for class variables and class variables set the initial value

      The variables used in the process of the memory area will be allocated in this case includes only memory allocation class variables, instance variables and does not include (instance variables will be assigned when the object is instantiated along with the objects in the Java heap) , 

      Further, static class variables assigned here is the value defined as the default value, the initial value is usually set here is the data type of a default value of zero (e.g., 0, 0L, null, false, etc.), rather than in Java code

      Explicitly given value, the correct assignment will be performed during the initialization phase,

    Parsing the binary data class is replaced symbolic references direct reference

      For example, the operational method of the class, calculating a = 1 symbol is removed directly into a 1, this can save a lot of resources

 

 

Initialization is a static class variables, static code block performing the initialization operation

      Class initialization phase is the last step in the class loading process, the above class loading process, in addition to load (Loading) phase of the user application can customize the out class loader participation, the remaining operation entirely by the virtual machine dominate and control the initialization stage, really started the class defined in the Java code 

  Initialization given for the class static variable correct initial value, the JVM is responsible for the class to initialize the main class variables are initialized, the class variable initializers in Java in two ways:

    • Specifies the initial value when you declare a static variable (class variables) 
    • Using static code block is assigned an initial value for class variables

        Initialization steps:

            1. If the class has not been loaded and connected, and then connected to a program loads the class

            2. If the direct parent class has not been initialized, it first initializes its direct parent

            3. If the class initialization statements, the system sequentially performs the initialization statements

  JVM create objects in the heap memory, the class member variables into the heap memory, assign default values 

  

  

Finally, it is the familiar Runtime runtime phase

Person p = new Person();
p,study();

Executing the code in the heap memory to create an object of the Person class, and a stored reference variable p space for the Person type stack memory allocation, p storing the address of the object and points to the object, call the study method p is actually the object access methods to study the bytecode region by Person Person object class bytecode

 


 

Glossary  :

Java source code: the Java source code, programs written in java language

Java class loader (Java Classloader): is part of the Java Runtime Environment (JRE), is responsible for dynamically load Java classes into the JVM memory space

JRE: namely Java Runtime Environment, J AVA operating environment, internal contains a Java virtual machine as well as a number of standard libraries (Jar package)

JAR package:  usually used for polymerization of a large number of Java class files, related metadata and resources (text, images, etc.) files into one file, in order to develop Java platform software application or library

JVM:  namely Java Virtual Machine capable of running Java bytecode ( Java bytecode) Virtual Machine

Class (Class):  a set of class objects having common attributes and behavior, class defines the properties and methods

Byte code:  bytecode is already compiled, but not with a specific machine code, the interpreter needs to be translated to machine code intermediate code

Java bytecode: an instruction format executed by the Java Virtual Machine

Java compiler: Java source files (.java files) compiled into bytecode files (.class files, is a special binary files, binary byte code file), this is the JVM bytecode "machine language" javac command can be seen as a simple Java compiler

Java interpreter: is part of the JVM, Java interpreter to explain the execution of the program after the Java compiler, java command can be seen as a simple Java interpreter

Runtime class : bytecode file is loaded into memory corresponding class runtime class is called, this is the runtime class of an instance of Class

Java heap (Heap): In the JVM create startup , it is the JVM largest block of memory being managed in the JVM , the heap (Heap) is available to run each time threads shared memory area is available to all classes and instances regional array of objects allocated memory, Java heap is shared by all threads in an area of memory, Java heap is the main area managed by the garbage collector

Java stack (Stack):  basic types of variables defined in the function, Java instruction code, object reference variables are allocated in the stack memory function, when exceeding the scope of variables, Java automatically allocated to free up the variable memory space

The method area (Non-Heap):   the method area and the Java heap, as each thread is a shared memory area for storing such information has been loaded in the virtual machine (construction methods and interface definitions), constants, static variables, time compiler compiled code data and the like, there is a method runtime constant pool area

Guess you like

Origin www.cnblogs.com/yangshaox/p/11611696.html
Recommended