4. 系统自带的类加载器和自定义类加载器区别

本文深入探讨了Java中类加载器的工作原理,包括系统自带类加载器与自定义类加载器的区别,以及它们如何加载同一类产生不同结果的原因。通过代码示例,详细解析了ClassLoader的loadClass方法,并对比了不同类加载器加载同一对象的过程。

1.代码解析

  1. 我们先来看一下这段代码,注意包名和类名,此java文件编译通过,但是运行会报错
package java.util;

public class List {

    public static void main(String[] args) {
        System.out.println("List类");
    }
}

在这里插入图片描述
2. 这是为什么呢?我们来看一段源码,这是ClassLoader的loadClass方 法。解释就是第一次加载一个类时,类加载器会调用父加载器去加载这个类

protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                try {
                    if (parent != null) {
                        c = parent.loadClass(name, false);
                    } else {
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {
                    // ClassNotFoundException thrown if class not found
                    // from the non-null parent class loader
                }

                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    long t1 = System.nanoTime();
                    c = findClass(name);

                    // this is the defining class loader; record the stats
                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }

2. 使用不同类加载器加载对象

这段代码表示,我们使用系统自带的类加载器,和使用自定义类加载器去加载同一个类,比较得到的结果。

package cn.mytest;

import java.io.IOException;
import java.io.InputStream;

public class ClassLoaderTest {
    public static void main(String[] args) throws ClassNotFoundException {
        //自定义类加载器
        ClassLoader loader = new ClassLoader() {
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                try {
                    String fileName = name.substring(name.lastIndexOf(".")+1)+".class";
                    InputStream is = getClass().getResourceAsStream(fileName);
                    if(is == null){
                        return super.loadClass(name);
                    }
                    byte[] b = new byte[is.available()];
                    is.read(b);
                    return defineClass(name,b,0,b.length);
                }catch (IOException e){
                    throw new ClassNotFoundException();
                }
            }
        };
        String className = "cn.mytest.ClassLoaderTest";
        //使用系统默认加载器
        Object obj1 = ClassLoaderTest.class.getClassLoader().loadClass(className);
        //使用自定义加载器
        Object obj2 = loader.loadClass(className);

        System.out.println(obj1 == obj2);
        System.out.println(obj1.equals(obj2));

    }
}

3.结果

结果说明使用系统自带的类加载器,和使用自定义类加载器加载同一对象,得到的对象地址不一样,内容也不一样,即完全不同

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼鱼大头鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值