Good programmers to learn Java JVM class loading mechanism to share routes

Good programmers to learn Java JVM class loading mechanism to share routes, JVM related concepts

  • <br> the JDK
    the JDK (Java Development Kit) Java Development Kit, it is a collection of Java developers a set of procedures for compiling and debugging of programs.
  • <br> jre
    jre (Java Runtime Evironment) Java Runtime Environment, is a platform for running Java programs, all Java programs can only be executed in this platform.
  • <br> the JVM
    the JVM (the Java Virtual Machine) the Java Virtual Machine, is out of the virtual computer codes to simulate the functions performed by a computer, it has its own hardware architecture, such as: processor, stack, registers, etc., as well as their own a set of instruction on different operating systems can be installed JVM, enabling Java programs can execute on different operating systems, JVM is to achieve Java's cross-platform features.
    Process JVM load the class
    we execute Java programs developed later, you need to compile and then executed, JVM is responsible for the process of loading the class. <br>
    process class loader is divided into:

    1. load
    2. verification
    3. ready
    4. Resolve
    5. Initialization
      particular class loading procedure
      described in detail below under these process:
    6. <br> loaded
      in the process of loading the class to complete:
    7. According to the full name of the qualifier class, obtain class binary stream, the stream can, jar files obtained from the class on the disk, can also be obtained from the network.
    8. Dynamic storage structure when the structure of the class of static storage operation into the method area
    9. In the heap memory to generate the corresponding java.lang.Class objects, a method as an inlet region
    10. Verify <br>
      After loading the class is completed, enter the verification process, which ensures that the information generated in the previous Class object will not endanger the safety of the JVM. <br>
      aspects need to be verified are:
    11. File format validation is to verify compliance with specifications Class byte stream file format, and can be the current version of virtual machine processing. The magic number, to verify whether 0xCAFEBABE; the major and minor version number is the current virtual machine processing range; if there is not constant type supported constant in the constant pool, etc., the main purpose is to ensure the verification phase input byte flow properly parse and stored in the process region, are authenticated at this stage, the byte stream will enter the storage method of the memory area, so that the latter three are based verification phase memory area configuration method performed.
    12. Metadata verification information is described bytecode semantic analysis, to ensure compliance with the information which describes the Java language specification. The validation may include: whether a parent class; whether the parent of this class inherit the inherited class allowed; if the class is not an abstract class, whether implements all the methods of its parent class or interface implementation requirements.
    13. Bytecode verification, the main task is the data flow and control flow analysis to ensure the method validation class at runtime will not make a virtual machine acts endangering safety. If the byte code method body does not pass a class bytecode verification, it is certainly a problem; but if a method body by the byte code verifier can not explain it necessarily safe.
    14. Verification of symbolic references, will take place in the virtual machine when symbolic references into direct reference, this conversion action will take place in the "analytical phase". Symbolic reference classes, fields and methods; validation symbol strings described by reference to the naming authority if a corresponding class can be found; whether there is a method in compliance with a method specified field descriptor class name and a simple description of the fields and accessibility (private, protected, public, default) whether the current class can be accessed.
    15. Preparation <br>
      preparation phase method allocates memory area for static class variables and assign default values.
      public static int count = 100;

      Such as: the above count variable in the preparation phase will be assigned to 0 and then assigned to 100 during initialization;

    16. <br> resolve
      the parsing stage is a virtual machine to a constant pool of symbolic references to the process of replacing direct references.
      • Reference symbol (Symbolic Reference) <br>
        symbolic reference to a certain set of symbols described in the referenced, literal symbols may be in any form, can be positioned unambiguously as long as the goal to use. Symbol reference has nothing to do with the memory layout to achieve a virtual machine, the goal is not necessarily a reference has been loaded into memory.
      • A direct reference (Direct Reference) <br>
        direct reference may be a direct pointer to the object, a relative offset or indirectly targeted to handle targets. Direct reference to the memory layout is related to implementation of the virtual machine, if you have a direct reference, the target reference must already exist in memory.
    17. Initialization <br>
      class initialization is the final step in the process of loading the class, in front of the class loading process, in addition to other than by self-defined class loader to participate in the remaining action is completely dominated by the virtual machine control and user applications in the loading phase. To the initialization phase, really started classes defined in Java code. <br>
      initialization phase is performed during the class constructor <clinit> () method. <clinit> () method is collected automatically by the compiler class assignment operation of all classes and static variables of the block of statements (static {} block) merge statements generated.
      So when to perform initialization it?
    18. Create an instance of the class
    19. Access class static variable (except the constant, final modified)
      reason: Constant a special variable, because the compiler treats them as property values rather than to treat.
    20. Access class static method
    21. The reflector (Class.forName ( "com.test.Person"))
    22. When initializing a class, the class has not yet found his father initialization, the first call to initialize the parent class
    23. When the virtual machine starts, the definition of the main () method to the class initialization
      code that case
      to understand the class loading mechanism, we look at a face questions:

      public class MySingleton {
      private static MySingleton singleton = new MySingleton();
      public static int count1 = 0;
      public static int count2;
      
      private MySingleton(){
              count1++;
              count2++;
      }
      
      public static MySingleton getInstance(){
              return singleton;
      }
      
      public static void main(String[] args) {
              MySingleton singleton = MySingleton.getInstance();
              System.out.println("count1-->"+MySingleton.count1);
              System.out.println("count2-->"+MySingleton.count2);
      }
      }
      上面的结果,大多数同学可能认为两个静态变量都是1,结果比较意外:

      count1-->0
      count2-->1

      
      这是为什么呢?下面我们来分析下:
    24. First we know will assign default values for static variables in the preparation phase class: <br>
      Singleton = null;
      count1 = 0;
      count2 = 0;
    25. When the getInstance static method call class, class initialization triggered, perform new MySingleton () constructor method call, then: <br>
      count1 is =. 1;
      count2. 1 =;
    26. Continue to initialize, assign values to variables, count1 assigned 0, count2 no assignment to retain the value 1, the result is: <br>
      count1 = 0;
      count2 = 1;
      summary
      JVM is the code of the computer simulation, with its own hardware and software, JVM to achieve load and run Java classes, there are specific loading process: loading, validation, preparation, analytical, initializing five steps.

Guess you like

Origin blog.51cto.com/14479068/2439170