本文共 1558 字,大约阅读时间需要 5 分钟。
众所周知,Java 是面向对象的一门语言
绝大部分的东西都是以class类的形式存在的
甚至连把.class文件加载进JVM的类加载器(class loader)都是一个类
那有人就会问了,那第一个类加载器是怎么来的呢?
这个和鸡生蛋,蛋生鸡的问题就不一样了~
这个是有明确答案的
首先,JVM 是用C++ 写的(这里指的是 SUN 的默认JVM HotSpot)
(维基百科上查的)
HotSpot is written in C++
(大家可以在命令行下输入)
java -version
应该会看到有这个内容
Java HotSpot™
具体关于HotSpot 的信息大家可以上维基百科去查,我这就不展开了
其次,第一个类加载器有挺多名称的—
Bootstrap classloader
Primordial classloader Null classloader指的都是一个东西
它也不是用Java 写的,来看看维基百科原文
The bootstrap class loader loads the core Java libraries located in the <JAVA_HOME>/jre/lib directory.
This class loader, which is part of the core JVM, is written in native code.
使用机器码写的
当JVM启动的时候,bootstrap class loader 也启动,并加载另外两个重要的类加载器----
Extensions class loader 和 System class loader
这两个类加载器又由sun.misc.Launcher$ExtClassLoader 和sun.misc.Launcher$AppClassLoader分别实现
他们就是用Java 写的啦~
而且我们也能切实看到他们
来看下面的代码:
public static void main(String [] agrs) { System.out.println(JSONObject.class.getClassLoader()); System.out.println(JSONObject.class.getClassLoader().getParent()); System.out.println(JSONObject.class.getClassLoader().getClass().getClassLoader()); System.out.println(String.class.getClassLoader()); }
输出结果:
sun.misc.Launcher$AppClassLoader@73d16e93
sun.misc.Launcher$ExtClassLoader@6bc7c054 null null
从输出结果可以看出:
总结,JVM中的类都是由类加载器加载的
第一个类加载器是bootstrap class loader
它还加载出了其他的类加载器,分别负责加载其他不同的类
转载地址:http://xoiyb.baihongyu.com/