Home  >  Article  >  Java  >  What are the changes to class loaders in Java 9?

What are the changes to class loaders in Java 9?

WBOY
WBOYforward
2023-08-30 15:37:02697browse

Java 9中的类加载器有哪些变化?

All java programs run on Java Virtual Machine (JVM). After compilation, java classes are converted to platform and machine-independent bytecode , and the compiled classes are stored as .class files. Whenever we try to use it, ClassLoader will load the class into memory. These classes are introduced into the Java environment when they are referenced by name. Once a class starts running, the loading of the class is done by the class loader, and the main() method is one way to start the class.

There are some minor changes to class loaders in Java 9: ​​

  • The system class loader no longer exists in Java 9; it is An instance of >URLClassLoader, is an inner class. It is the default loader for classes in modules.
  • Extension ClassLoader has been renamed to Platform ClassLoader. All classes in the Java SE platform are visible through the platform class loader, and classes that are in modules under the Java community process but are not part of the Java SE platform are also visible through the platform class loader.
  • Applications cannot depend on which platform class loader is defined, some classes in the Java SE platform are defined by the platform class loader, while other classes are Bootstrap class loaderdefined.
  • If the class loader created by the existing code uses the bootstrap class loader as the parent class loader, then we need to change to use the platform class loader as the parent Class loader.
  • Platform Class Loader is not an instance of URLClassLoader, but an internal class.
  • Bootstrap class loader is a >JVM's built-in class loader. However, it defines classes for key modules, such as Base. Applications deployed using -Xbootclasspath/a or applications using null as parent to create a classloader may need to be changed.
  • ul>

    Example

    public class ClassLoaderTest {
       public static void main(String args[]) {
          System.out.println("Class Loader Test");
          ClassLoaderTest test = new ClassLoaderTest();
          try {
             test.showClassLoaders();
          } catch(ClassNotFoundException cnfe) {
             System.out.println(cnfe.getMessage());
          }
       }
       public void showClassLoaders() throws ClassNotFoundException {
          System.out.println("Classloader of this class: " + <strong>ClassLoaderTest.class.</strong><strong>getClassLoader()</strong>);
          System.out.println("Classloader of Permission: " + <strong>java.sql.SQLPermission.class.</strong><strong>getClassLoader()</strong>);
          System.out.println("Classloader of LinkedList: " + <strong>java.util.LinkedList.class.</strong><strong>getClassLoader()</strong>);
          return;
       }
    }

    Output

    <strong>Class Loader Test
    Classloader of this class: jdk.internal.loader.ClassLoaders$AppClassLoader@504bae78
    Classloader of Permission: jdk.internal.loader.ClassLoaders$PlatformClassLoader@299a06ac
    Classloader of LinkedList: null</strong>

The above is the detailed content of What are the changes to class loaders in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete