• Java EKT关键技术强化 第01章 类加载器


    运行源码:

    package com.ChenJiangTing.Demo4;
    
    public class Demo4 {
        public static void main(String[] args) {
            Class clz;
            ClassLoader cl, cll;//类加载器
    
            clz=String.class;
            cl=clz.getClassLoader();//得到加载String类对象的类加载器
            System.out.println("加载String类对象的加载器:" + cl);
            //当类加载器为null的时候,表示的是BootStrapLoader
    
            System.out.println("******************************");
            clz=Demo4.class;
            cl=clz.getClassLoader();
            System.out.println("加载Demo4类对象的类加载器:" + cl);
            while (cl!=null){
                cll=cl;
                cl=cll.getParent();//得到父加载器
                System.out.println(cll+"的父加载器是:" + cl);
            }
    
        }
    }

    运行结果:

    加载String类对象的加载器:null
    ******************************
    加载Demo4类对象的类加载器:sun.misc.Launcher$AppClassLoader@14dad5dc
    sun.misc.Launcher$AppClassLoader@14dad5dc的父加载器是:sun.misc.Launcher$ExtClassLoader@28d93b30
    sun.misc.Launcher$ExtClassLoader@28d93b30的父加载器是:null

    技术总结:

    1.Class对象由JVM自动产生,每当一个类被加载时,JVM就自动为其生成一个Class对象,通过Class对象可以获得类的相关信息。 将类信息读到内存中过程,称为类加载。

    2.获得类的基本信息的常用方法:

      1)getName() 获得类名 getPackage()

      2)获得包名 isInterface()

      3)是否为接口 isPrimitive()

      4)是否为基本类型 isArray()

      5) 是否为数组 getSuperclass()

      6)获得父类的Class对象

    3.这6种情况下JVM会加载Class对象

       一、使用new关键字创建对象时。

       二、通过反射创建对象时。

      三、调用某个类的static方法时。

       四、调用某个类的static属性时。

      五、当初始化某个类的子类时。

      六、某个类标明为main() 函数类(启动类) 。

  • 相关阅读:
    C++中四种类型转换方式
    LeetCode——Move Zeroes
    LeetCode——Construct the Rectangle
    LeetCode——Add Digits
    LeetCode—— Invert Binary Tree
    LeetCode——Sum of Two Integers
    LeetCode——Find the Difference
    LeetCode——Maximum Depth of Binary Tree
    kafka 安装及部署
    apache kafka系列之server.properties配置文件参数说明
  • 原文地址:https://www.cnblogs.com/CjtBlog/p/13537097.html
Copyright © 2020-2023  润新知