Deep Java Virtual machine-Class loader

Source: Internet
Author: User

This series learns notes for deep Java virtual machines (Zhou Zhiming)

  1. The code module that obtains a binary byte stream that describes this class is called a class loader through the fully qualified name of a class.
  2. For any class, it is necessary to establish its uniqueness in the Java Virtual machine, together with the class loader that loads it and the class itself. Popular understanding: Compare two classes is "equal", only if the two classes are the same loader load the premise of meaning, otherwise, even if the two classes from the same class file, is loaded by the same virtual machine, as long as loading their class loader different, then the two classes must be unequal. The "equality" here includes the return result of the Equals () method, the IsAssignableFrom () method, the Isinstance () method of the Class object, and the Instanceof keyword for object dependency determination. Examples are as follows:
  3. class loader classification
    from the JVM perspective is divided into two:
    One is to launch the ClassLoader (Bootstrap ClassLoader), implemented in the C + + language, is part of the virtual machine. The
    other is all other ClassLoader, all implemented by the Java language, independent of the outside of the virtual machine, inherited from the abstract class Java.lang.ClassLoader. The angle of the

    JAVA program is broadly divided into three categories:
    Startup ClassLoader (Bootstrap ClassLoader): Responsible for storing in the <java_home>\lib directory, The class library that is recognized by the Java Virtual machine is loaded into memory in the virtual machine, either in the path specified by the-xbootclasspath parameter. cannot be referenced directly by a Java program. The
    Extension ClassLoader (Extension ClassLoader) is responsible for loading all class libraries in the <java_home>\lib\ext directory, or in the classpath specified by the Java.ext.dirs system variables, which developers can use directly.
    Application ClassLoader (Application ClassLoader): is responsible for loading the class library specified on the user classpath (ClassPath), which can be used directly by the developer, if there is no custom ClassLoader, typically the default class loader for the program.

  4. Parent delegation model for class loader
    The parental delegation model requires that the rest of the ClassLoader should have its own parent classloader, in addition to the top-level startup class loader. The parent-child relationship here typically uses a composite relationship to re-use the parents classloader.
    Parent delegation Model work process: If a class loader receives a request for a class load, it first does not attempt to load the class itself, but the request is delegated to the parent ClassLoader to complete, and every layer of the classloader is so, so all the load requests should eventually be routed to the top-level startup class loader. The subclass loader tries to load itself only if the parent ClassLoader feedback that it cannot complete the load request (its search scope does not find the desired class).
    The benefit of the parental delegation Model: The Java class has a hierarchical relationship with precedence as its classloader. For example, Java.lang.Object, which is stored in Ra.jar, regardless of which class loader needs to load the class, is ultimately delegated to the startup ClassLoader at the top of the model to load, so the Objcet class is the same class in the various class loading environments of the program. Conversely, if the parent delegation model is not used, it is loaded by the various classloader. If the user writes a Java.lang.Object class, put in the program's classpath, there will be a number of different object classes in the system, the Java type System of the most basic behavior can not be guaranteed.
    Parental delegation Model implementation: first check whether it has been loaded, if not loaded, call the parent loader's Findclass () method to load, if the parent loader is empty, use the Startup class loader as the parent loader by default. If the parent class fails to load, the ClassNotFoundException exception is thrown, and then the Findclass () method is called to load

Deep Java Virtual machine-Class loader

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.