3. Creating an object

Create three objects

Object creation

Class loading -> Object Space Allocation -> Set object header -> object initialization

Class loader

  • java class loading connection initialization is done during program execution.

  • Lifecycle class: load , validate analytical preparation (both belong to the connection process), initialization , use , unloading .

  • Resolution May after initialization, other stages may be carried out cross.

  • Initialization start time

    • new new , call the static method , reflection calls , initialize the subclass when the first trigger initialization of the parent class , initialize the main method in the main class , the use of dynamic language support .
    • Note: The above is the one and only, so for example, does not initialize the subclass when called by the parent class static variable subclass initialization only parent class, declare an array of objects does not cause constant initialization, call the class does not cause initialization.
    • Interface initialization and class distinction: the same kind initialization time, but does not require sub-interface initialization of the parent class interface initialization.
  1. load

    • Class file obtained by the full name of the class defining binary stream - "flow of the byte in the method area -" generates a representative of the class in object such as the various methods of data access region of the inlet of the class (this object does there must be possible in the heap area method).
    • Different array class is loaded, created by the array class without jvm class loader, if the storage array is a reference type is called class this reference loading process, and on this array class identifies the class loader reference to the class. If the reference type but not for basic types int [] array will be an array class mark to the bootstrap class loader. Element array class class visibility and visibility in the same array, the default basic type is public.
    • Loading and verification cross, validation could begin the loading process.
  2. Connection - verification

    • Ensure that the information class byte stream is safe to meet the requirements. So it is closed most of the validation process.

    • Verify file format -> metadata validation -> bytecode verification -> verification of symbolic references

    • File format validation

      • The main object of the guarantee can correctly parse the byte stream and stored in the method area. After the adoption of this method validation will enter the storage area.
      • Whether that begins magic number (in order to confirm whether the class file), major and minor version number can be run on the current jvm, whether there is a constant type (check tag flag) is not supported, is there a point to point to the constant index values there is no constant or does not meet the type of constant, if there is to be deleted or added other information. and many more
    • Metadata validation

      • Information bytecode semantic analysis (data type check) to ensure compliance with specifications java language.
        • Is there a parent (except object of all classes must be made by the parent class)
        • It is not allowed to inherit the inherited class
        • Whether to implement the method of the parent class or interface required to achieve (except abstract class interface)
        • The existence of non-compliance or heavy-duty coverage
    • Bytecode verification

      • Determine the program semantics legitimate way to experience certificate.
        • Data type stack instruction code sequence corresponding to
        • The method does not jump instruction jump member
        • Body type conversion method is effective b
    • Verification of symbolic references

      • Reference symbol into direct verification with reference to the occurrence of the parsing stage .
      • Information other than its own class (reference symbol the constant pool) for matching verification
        • Whether the fully qualified name of the reference symbol corresponding class can be found in
        • Whether there is described methods and fields conform to the specified class
        • Accessible symbolic reference classes, fields, methods (privatepublic) can access
  3. Connection - ready

    • Officially assigned a class variable class variable memory and set the initial value (the initial value of this type of the initial value defined by the non-initial value) of the stage, these variables are allocated to the zone method.
    • Class constants given directly user-defined value types rather than the initial value.
  4. Resolve

    • Symbol reference is converted to a direct reference to the stage.
      • Symbol references
        • Use symbols to represent a reference target, the target references may not be loaded into memory, regardless of the memory layout.
      • Direct reference
        • Directly to the destination pointer relative offsets can handle other targets, the target reference is already present in memory.
    • The determination may be resolved when the class loader needs to be used or wait until resolved.
  5. initialization

    • Initialization phase in the implementation of the code is really class. Preparation phase has been assigned to the class variable default initial value of the stage will be responsible for java class variable conferred on the value defined in the code. Or is clinit method for performing a class constructor.

      • clinit method is generated by the compiler.

      • Method of composition clinit

        • Java code sequence by the code blocks and static variables defined by the class consisting of

        • static {
              i=0;
              System.out.println(i);//错误示范
          }
          int i=2;
          //注意此类中静态代码块在前,静态代码块中引用的变量在代码块后定义,但是因为变量是在准备过程中分配的空间,因此可以对i进行赋值,但是不可以进行访问。
        • clinit first parent class method are performed after the subclass. Note: But clinit parent interface method is performed only if you are using a variable of the parent class.

    So far class loading process is complete.

  6. Class loader

    • jvm by the fully qualified name of the class loading during operation of obtaining a binary stream into jvm implemented externally, can be added to customize how to obtain a binary stream class.

    • Class loader functions in addition to the loading of further configured to determine a unique class. Uniqueness of the class by the class loader itself and determined together. The only kind of sex can lead to class object equals method, isAssignableFrom method, isInstance method, instanceof keyword results returned.

    • Classification class loader

      • Start class loader Bootstrap class loader, c ++ implementation. Instead of using null java.
      • Other class loader, java achieve, inherited from java.lang.ClassLoader.
        • Extension class loader
        • Application class loader
        • User-defined class loader
      • Parent delegation model
        • Delegate to the class loader class loader's parent class loader, if the failure by the parent class loader class loader then loads.
        • It is destroyed: as jndi service, which is the code that needs to call a class loaded by the boot loader but therefore start class loader can not load the classpath, they introduced a thread context loader to destroy parent delegation model.

Object space allocation

  • Began for the new object allocated on the heap memory, the memory size will be determined at the time the class is loaded after the class load, so only the heap memory space can be divided.

  • Memory allocation:

    • Pointer collision
      • If the stack is structured using a pointer, the left pointer has been assigned to the object, the right side is the free space.
    • Free list
      • If the pile is irregular, then using free list of available memory storage area, to choose the size of the memory area in line with the allocation.
    • Collision or idle pointer list depends on whether the garbage collector with sorting function.
    • Because memory is allocated thread safe, it is generally employed cas plus failure retry way to guarantee atomicity operations, and the thread is allocated in advance for its own space to be avoid synchronization problems TLAB buffer allocated by the local thread. And when their own space enough then retry cas + expansion to guarantee atomicity.

Object Memory layout

  • Memory layout hotspot virtual machine is divided into objects in the object header, instance data, three alignment padding portion.
  • Object header
    • With the same region of the virtual machine MarkWord bits, for storing runtime data.
      • Such as: hash code, gc age, lock status, and so on.
    • Pointer type, a pointer to its class object is metadata. (Whether there is a pointer to the class metadata virtual machine implementation depends, as not to find the presence of class information corresponding to the object by the object)
  • Examples of data
    • Various types of content code defined in the general classification by order placement. Data including inherited parent class.
  • Alignment padding
    • Object Memory must be an integer number of bit.

Object Initialization

The metadata pointer depends virtual machine implementation, there is no way to find the class information corresponding to the object by the object)

  • Examples of data
    • Various types of content code defined in the general classification by order placement. Data including inherited parent class.
  • Alignment padding
    • Object Memory must be an integer number of bit.

Object Initialization

  • Execution object's init method to initialize the assignment of the object field. Previously fields are 0.
Published 27 original articles · won praise 1 · views 901

Guess you like

Origin blog.csdn.net/hu853996234/article/details/103736343