In-depth Java Virtual machine video tutorials (Saint Stone)--Learning Notes

Source: Internet
Author: User
This video is called Deep Java Virtual machine, it should be called Java class ClassLoader in-depth video. Review the ClassLoader is related to knowledge 1, class loading, connection, and initialization loading: Finding and loading binary data connections for classes: validating: Ensuring that the loaded class is ready for correctness: allocates memory for the class's static variables and initializes it to the default value resolution: Class For direct reference initialization: Assigning the correct initial value for a class's static variable
2, Java program to use the class can be divided into two kinds of active use of passive use of all Java Virtual Machine implementation must be in each class or interface by the Java program for the first active use of the initialization of 3, six cases of active use create an instance of a class to access a static variable of a class or interface, or to assign a static variable to a class Calling static method reflection of a class: Class.forName Initializes a subclass of a class Java virtual machine started with a class that was marked as the starting class in addition to the above six scenarios other uses of Java classes are considered to be the passive use of the class, which does not result in initialization of classes 4, class loading Class loading refers to reading binary data in the. class file of the class into memory, placing it in the method area of the Run-time data area, and then creating a Java.lang.Class object in the heap area to encapsulate the data structure of the class within the method area. The final product of a class's loading is the class object class object in the heap area that encapsulates the data structure of the class within the method area and provides Java programmers with an interface to access data structures within the method area.
5, Loading. class files are loaded directly from the local system through the network download. class files are loaded from archive files such as Zip,jar. class files are extracted from the proprietary database. class file dynamically compiles Java source files into. Class 6, There are two types of ClassLoader Java Virtual machine with loader   root class loader (Bootstrap)   Extended class loader (Extension)   System class loader (systems)   also called application loader User-defined ClassLoader  java.lang.classloader subclasses   Users can customize the way classes are loaded if the class is loaded by the root ClassLoader, class.getclassloader () returns NULL, for example Class.forName ("Java.lang.String"). getClassLoader () gets a null class loader that does not need to wait until a class is first actively used and then load it The JVM specification allows the ClassLoader to preload a class when it is expected to be used, and if it encounters a. class file missing or error in the preload process, the class loader must report an error (Linkageerror error) when the program first actively uses the class If the class has not been actively used by the program, then the ClassLoader will not report error 7, the class's validation class is loaded, and then enter the connection phase. A connection is the merging of binary data from a class that has been read into a virtual machine into a running environment in a VM. Validation of class content   class file structure check: Ensure class files comply with Java class file fixed format   Semantic check: Ensure class itself conforms to Java syntax rules, For example, verify that the final type of class has no subclasses, and that the final type of method is not overwritten.   Bytecode verification: ensure that bytecode streams can be safely executed by Java virtual machines, bytecode streams represent Java methods (including static methods and instance methods)       It is a sequence of single-byte instructions called opcode. Each opcode follows one or more operands, and the bytecode verification       steps verify that each opcode is legitimate and that there is a legitimate operand   binary compatibility verification: ensure consistency between classes that reference each other, For example, in the Gotowork method of worker, call the Run method of the     car class, the Java virtual machine checks the worker class to see if there is a run method in the method, &nbsp   If it does not exist, it throws a nosuchmethoderror error. 8, the preparation phase of the class in the preparation phase, the Java Virtual machine for the class of static variables allocated memory, and set the default initial value of 9, class parsing phase in the parsing phase, Java Virtual opportunity to replace the class's binary data in the symbolic reference to the direct reference, for example The Gotowork method of the worker class refers to the Carl class Run method, which contains a symbolic reference to the run method of the car class in the binary data of the work class, which consists of the full name of the Run method and the associated descriptor, in the parsing phase, The Java virtual opportunity replaces this symbolic reference with a pointer to the memory location of the car class's Run method in the method area, which is a direct reference. 10, the initialization phase of the class in the initialization phase, the Java virtual machine executes the initialization statement of the class, giving the initial value to the static variable of the class, in which there are two ways to initialize the static variable: 1, initialize at the declaration of static variable, 2, initialize in the static code block. 11, class initialization step A, if the class is not loaded and connected, load and join first B, the class has a direct parent class, and the parent class is not initialized, first initialize the direct parent Class C, the class in the presence of initialization statements, and then execute the initialization of the statement compile the regular amount of time does not initialize the class, A non-compile constant initializes the class 12, class initialization time when a Java virtual machine Initializes a class, all of its parent classes are initialized, but this rule does not apply to an interface when initializing a class without initializing the interface it implements when initializing an interface. does not initialize its parent interface. Therefore, an interface is not initialized because of the initialization of its sub-interface or implementation class, which results in the initialization of the interface only when the program first uses the static variables of a particular interface. Only when a program accesses a static variable or a static method that is actually defined in the current class or interface can it be assumed that the active use of a class or interface loads a class with the LoadClass method of calling the ClassLoader class, not an active invocation of the class, which does not cause initialization of the class 13, class loader Class loaders are used to load classes into Java virtual machines, and the process of loading a class with a father-entrusted mechanism is a better way to secure the Java platform. In the delegate mechanism, the rest of the loader has and has only one parent loader in addition to the root class loader that is brought by the Java virtual machine. 14, Java Virtual machine with a few loader root class loader (Bootstrap): The loader does not have a parent loader. Responsible for loading virtual machine core library, such as java.lang.*. The root class loader specifies the Sun.boot.class.path from the system attributeRecord to load the class library. The implementation of the root class loader relies on the low-level operating system as part of the virtual machine implementation and does not inherit the Java.lang.ClassLoader class extension ClassLoader (Extension): His parent class loader is the root class loader, Loading the class library from the directory specified by the Java.ext.dirs system attribute or loading the class library from the Jre/lib/ext subdirectory of the JDK installation directory, if the user puts the jar package into this directory, is automatically loaded by the extended class loader. The extended class loader is a pure Java class, a subclass of the Java.lang.ClassLoader System class loader (systems): Also known as the application ClassLoader, his parent class loader is an extended classloader, he classpath from environment variables, Or in the directory specified by the system attribute Java.class.path, which is a user-defined ClassLoader, the System class loader is a pure Java class and a subclass of the Java.lang.ClassLoader class. 15, custom class loader In addition to the above virtual machine with the class loader, users can also customize their class loader, Java provides an abstract class Java.lang.ClassLoader, all user-defined ClassLoader should inherit from ClassLoader
16, the parent delegation mechanism in the parent delegation mechanism, each loader in a parent-child relationship to form a tree structure, in addition to the root class loader, the rest of the ClassLoader has and only one parent loader. If a class loader can successfully load a class, this class loader is called the definition ClassLoader, and all class loaders that successfully return a class object reference, including the definition class loader, are referred to as the parent-child relationship between the initial ClassLoader loader, which actually refers to the wrapper relationship between the ClassLoader objects. Rather than the inheritance relationship between classes. When a custom class loader instance is built, if his parent class loader is not specified, the System class loader is called the parent loader of the class loader. 17, the parent delegation mechanism can improve the security of the software system. Because of this load mechanism, user-defined class loaders cannot load trusted classes that should be loaded by the parent loader, thereby preventing unreliable or even malicious code from replacing reliable code loaded by the parent loader. 18, namespaces each class loader has its own namespace, namespaces are made up of classes loaded by the class loader and all the parent loaders in the same namespace, without the same two classes with the full name of the class; in different namespaces, two classes with the same name may appear. 19. The Run-time package consists of a class of the same package loaded by the same ClassLoader, which determines whether two classes belong to the same run-time package, not only to see if their package names are the same, but also to define if the class loader is the same. Only classes that belong to the same Run-time package can access the class and class members that are visible to the package. Such restrictions prevent user-defined classes from impersonating the core class library's classes to access the core library's package visible members.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.