What is the concept of class loading mechanism?

The JVM loads the data describing the Class from the Class file into memory, validates, transforms, and initializes the data, and eventually forms Java types that can be used directly by the virtual machine. This is the class loading mechanism.

What are the components of class 2 bytecode?

There are only two types of data in the Class file structure:

  1. Unsigned number: a basic data type. U1, U2, U4, and U8 represent unsigned numbers of 1, 2, 4, and 8 bytes respectively. They can be used to describe numbers, index references, quantity values, or utF-8 encoded string values.
  2. Table: A compound data type consisting of unsigned numbers or other tables as data items, all of which conventionally end with “_info”;
  3. So, a Class file is essentially a table;

The content composition of the Class file structure:

  1. JAVA magic number: CAFEBABE, a hexadecimal number, the only thing that determines whether the file is an accepted Class file by the virtual machine.
  2. Class file version: the 5th and 6th bytes of the Class file are the minor version number, and the 7th and 8th bytes are the major version number.
  3. Constant pool: Starting at byte 9, it contains two main types of constants: (1) literals; (2) symbolic reference;
  4. Access flags: The two bytes immediately following the constant pool represent access flags, representing hierarchical information about a class or interface;
  5. Class index, superclass index and interface index collection: Following the access flag is the Class index, superclass index and interface index, which are u2 type data. These three data are used to determine the Class inheritance relationship in the Class file.
  6. Field table collection: This is followed by a field table collection that describes variables declared in an interface or class;
  7. Method table collection: This is followed by the method table collection, which describes methods declared in interfaces or classes.

What are the three types of loading processes?

When will class loading start?

  1. Create an instance of a class: instantiate an object using the new keyword;
  2. Access static variables of a class: getStatic or putStatic, read or set static variables of a class (excluding static variables modified by final);
  3. Invokestatic Invokestatic invokes static methods of a class;
  4. Use java.lang.reflect to make a reflection call: for example, class.forname (” XXXXX “);
  5. When a subclass is initialized, its parent class is initialized first.

5 How many passive references are there? Do passive references not trigger class initialization?

  1. Using a subclass to refer to a static field of the parent class does not cause the subclass to initialize, only the parent class;
  2. Referencing a class through an array definition does not trigger class initialization;
  3. Constants are stored in the constant pool of the calling class at compile time, and are not directly referenced to the class that defines the constant, since the class that defines the constant is not initialized.

Class loading: What is the process of the first loading stage?

  1. Gets the binary stream that defines this class by its fully qualified name;
  2. Transform the binary stream static structure into a method area runtime data structure;
  3. Generate a java.lang.Class object in memory that represents this Class. For the HotSpot VIRTUAL machine, the Class object is stored in the method area;

Class 7 loading: Step 2 Verification stage specific process?

The main function is to ensure that the byte stream in the Class file meets the requirements of the current VM and does not harm vm security.

  1. File format verification: check format and version;
  2. Metadata validation: semantic analysis of bytecode;
  3. Bytecode verification;
  4. Symbol reference validation;

Class loading: Step 3 Preparation Stage Specific process?

  1. Allocate memory for class variables, which are static modified variables, in the method area.
  2. Public static int value = 123; public static int value = 123; The initial value is 0, not 123, and the assignment of 123 will only be performed during initialization;

Class loading: What is the process of step 4?

Replace symbolic references in the constant pool of the Class file with direct references;

Class 10 loading: Step 5 Initialize the specific process?

To actually start executing the Java program code defined in the class, initialization is the process of executing the class constructor () method;

  1. The () method automatically collects assignment actions and static code blocks for all class variables in the class, in the order in which the code appears;
  2. The () method, unlike the class instance constructor, guarantees that the () method of the parent class is executed before the () method of the subclass is executed.

How many class loaders do you have?

  1. Bootstrap ClassLoader: it is responsible for loading Java core classes, such as String and System classes.
  2. Extension ClassLoader: the Extension library responsible for loading the JRE;
  3. The System ClassLoader is responsible for loading the JAR packages and CLASSPATH specified by the CLASSPATH environment variable.
  4. User class loader: A user – defined loader that has the class loader as its parent

What is the parent delegate pattern of class loaders and why?

Parent delegation mechanism: if a class loader receives a request to load a class, it will not try to load the class itself, but will delegate the request task to the parent class loader to complete, recursively, if the parent class loader can complete the class loading task, it will return successfully; Only if the parent class loader is unable to complete the load task, do the load itself.

A Java class has a hierarchical relationship with priority along with the classloader that loads it. For example, the Object class in Java, which is stored in rt.jar, is delegated to the boot class loader at the top of the model by any class loader. Therefore, Object is the same class in any class loading environment. Without the parental delegation model, there would be many different Object classes in the system if each class loader picked them up and loaded them by itself.