classloader加载

 

class ClassA{
	public void method(){
     System.out.println("Loading ClassA");
	}
}
public class ClassLoaderTest {
	public static void main(String args[]){
     TestClassA testClassA = new TestClassA();
     testClassA.method();
	}
}
 

 

编译,执行

 

   java -verbose:class ClassLoaderTest

 

 

[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
[Loaded java.lang.reflect.AnnotatedElement from shared objects file]
[Loaded java.lang.Class from shared objects file]
[Loaded java.lang.Cloneable from shared objects file]
[Loaded java.lang.ClassLoader from shared objects file]
[Loaded java.lang.System from shared objects file]
[Loaded java.lang.Throwable from shared objects file]
[Loaded java.lang.Error from shared objects file]
[Loaded java.lang.ThreadDeath from shared objects file]
[Loaded java.lang.Exception from shared objects file]
[Loaded java.lang.RuntimeException from shared objects file]
[Loaded java.security.ProtectionDomain from shared objects file]
[Loaded java.security.AccessControlContext from shared objects file]
[Loaded java.lang.ClassNotFoundException from shared objects file]
[Loaded java.lang.LinkageError from shared objects file]
[Loaded java.lang.NoClassDefFoundError from shared objects file]
[Loaded java.lang.ClassCastException from shared objects file]
[Loaded java.lang.ArrayStoreException from shared objects file]
[Loaded java.lang.VirtualMachineError from shared objects file]
[Loaded java.lang.OutOfMemoryError from shared objects file]
[Loaded java.lang.StackOverflowError from shared objects file]
[Loaded java.lang.IllegalMonitorStateException from shared objects file]
[Loaded java.lang.ref.Reference from shared objects file]
[Loaded java.lang.ref.SoftReference from shared objects file]
[Loaded java.lang.ref.WeakReference from shared objects file]
[Loaded java.lang.ref.FinalReference from shared objects file]
[Loaded java.lang.ref.PhantomReference from shared objects file]
[Loaded java.lang.ref.Finalizer from shared objects file]
[Loaded java.lang.Runnable from shared objects file]
[Loaded java.lang.Thread from shared objects file]
[Loaded java.lang.Thread$UncaughtExceptionHandler from shared objects file]
[Loaded java.lang.ThreadGroup from shared objects file]
[Loaded java.util.Dictionary from shared objects file]
[Loaded java.util.Map from shared objects file]
[Loaded java.util.Hashtable from shared objects file]
[Loaded java.util.Properties from shared objects file]
[Loaded java.lang.reflect.AccessibleObject from shared objects file]
[Loaded java.lang.reflect.Member from shared objects file]
[Loaded java.lang.reflect.Field from shared objects file]
[Loaded java.lang.reflect.Method from shared objects file]
[Loaded java.lang.reflect.Constructor from shared objects file]
[Loaded sun.reflect.MagicAccessorImpl from shared objects file]
[Loaded sun.reflect.MethodAccessor from shared objects file]
[Loaded sun.reflect.MethodAccessorImpl from shared objects file]
[Loaded sun.reflect.ConstructorAccessor from shared objects file]
[Loaded sun.reflect.ConstructorAccessorImpl from shared objects file]
[Loaded sun.reflect.DelegatingClassLoader from shared objects file]
[Loaded sun.reflect.ConstantPool from shared objects file]
[Loaded sun.reflect.FieldAccessor from shared objects file]
[Loaded sun.reflect.FieldAccessorImpl from shared objects file]
[Loaded sun.reflect.UnsafeFieldAccessorImpl from shared objects file]
[Loaded sun.reflect.UnsafeStaticFieldAccessorImpl from shared objects file]
[Loaded java.lang.Appendable from shared objects file]
[Loaded java.lang.AbstractStringBuilder from shared objects file]
[Loaded java.lang.StringBuffer from shared objects file]
[Loaded java.lang.StringBuilder from shared objects file]
[Loaded java.lang.StackTraceElement from shared objects file]
[Loaded java.nio.Buffer from shared objects file]
[Loaded sun.misc.AtomicLong from shared objects file]
[Loaded sun.misc.AtomicLongCSImpl from shared objects file]
[Loaded java.lang.Boolean from shared objects file]
[Loaded java.lang.Character from shared objects file]
[Loaded java.lang.Number from shared objects file]
[Loaded java.lang.Float from shared objects file]
[Loaded java.lang.Double from shared objects file]
[Loaded java.lang.Byte from shared objects file]
[Loaded java.lang.Short from shared objects file]
[Loaded java.lang.Integer from shared objects file]
[Loaded java.lang.Long from shared objects file]
[Loaded java.io.ObjectStreamField from shared objects file]
[Loaded java.util.Comparator from shared objects file]
[Loaded java.lang.String$CaseInsensitiveComparator from shared objects file]
[Loaded java.security.Guard from shared objects file]
[Loaded java.security.Permission from shared objects file]
[Loaded java.security.BasicPermission from shared objects file]
[Loaded java.lang.RuntimePermission from shared objects file]
[Loaded java.util.AbstractMap from shared objects file]
[Loaded sun.misc.SoftCache from shared objects file]
[Loaded java.lang.ref.ReferenceQueue from shared objects file]
[Loaded java.lang.ref.ReferenceQueue$Null from shared objects file]
[Loaded java.lang.ref.ReferenceQueue$Lock from shared objects file]
[Loaded java.util.HashMap from shared objects file]
[Loaded java.util.Map$Entry from shared objects file]
[Loaded java.util.HashMap$Entry from shared objects file]
[Loaded java.security.AccessController from shared objects file]
[Loaded java.lang.reflect.ReflectPermission from shared objects file]
[Loaded java.security.PrivilegedAction from shared objects file]
[Loaded sun.reflect.ReflectionFactory$GetReflectionFactoryAction from shared objects file]
[Loaded java.lang.Iterable from shared objects file]
[Loaded java.util.Collection from shared objects file]
[Loaded java.util.AbstractCollection from shared objects file]
[Loaded java.util.List from shared objects file]
[Loaded java.util.AbstractList from shared objects file]
[Loaded java.util.RandomAccess from shared objects file]
[Loaded java.util.Vector from shared objects file]
[Loaded java.util.Stack from shared objects file]
[Loaded sun.reflect.ReflectionFactory from shared objects file]
[Loaded java.lang.ref.Reference$Lock from shared objects file]
[Loaded java.lang.ref.Reference$ReferenceHandler from shared objects file]
[Loaded java.lang.ref.Finalizer$FinalizerThread from shared objects file]
[Loaded java.util.Enumeration from shared objects file]
[Loaded java.util.Hashtable$EmptyEnumerator from shared objects file]
[Loaded java.util.Iterator from shared objects file]
[Loaded java.util.Hashtable$EmptyIterator from shared objects file]
[Loaded java.util.Hashtable$Entry from shared objects file]
[Loaded java.nio.charset.Charset from shared objects file]
[Loaded java.nio.charset.spi.CharsetProvider from shared objects file]
[Loaded sun.nio.cs.FastCharsetProvider from shared objects file]
[Loaded sun.nio.cs.StandardCharsets from shared objects file]
[Loaded sun.util.PreHashedMap from shared objects file]
[Loaded sun.nio.cs.StandardCharsets$Aliases from shared objects file]
[Loaded sun.nio.cs.StandardCharsets$Classes from shared objects file]
[Loaded sun.nio.cs.StandardCharsets$Cache from shared objects file]
[Loaded java.lang.ThreadLocal from shared objects file]
[Loaded java.util.concurrent.atomic.AtomicInteger from shared objects file]
[Loaded sun.misc.Unsafe from shared objects file]
[Loaded java.lang.IncompatibleClassChangeError from shared objects file]
[Loaded java.lang.NoSuchMethodError from shared objects file]
[Loaded sun.reflect.Reflection from shared objects file]
[Loaded java.util.Collections from shared objects file]
[Loaded java.util.Set from shared objects file]
[Loaded java.util.AbstractSet from shared objects file]
[Loaded java.util.Collections$EmptySet from shared objects file]
[Loaded java.util.Collections$EmptyList from shared objects file]
[Loaded java.util.Collections$EmptyMap from shared objects file]
[Loaded java.util.Collections$ReverseComparator from shared objects file]
[Loaded java.util.Collections$SynchronizedMap from shared objects file]
[Loaded java.lang.Class$3 from shared objects file]
[Loaded java.lang.reflect.Modifier from shared objects file]
[Loaded sun.reflect.LangReflectAccess from shared objects file]
[Loaded java.lang.reflect.ReflectAccess from shared objects file]
[Loaded java.lang.StringValue from shared objects file]
[Loaded java.util.Arrays from shared objects file]
[Loaded java.lang.Math from shared objects file]
[Opened C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar]
[Loaded java.nio.charset.Charset$3 from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar]
[Opened C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar]
[Loaded sun.nio.cs.AbstractCharsetProvider from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar]
[Loaded sun.nio.cs.ext.ExtendedCharsets from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar]
[Loaded java.lang.Class$1 from shared objects file]
[Loaded sun.reflect.ReflectionFactory$1 from shared objects file]
[Loaded sun.reflect.NativeConstructorAccessorImpl from shared objects file]
[Loaded sun.reflect.DelegatingConstructorAccessorImpl from shared objects file]
[Loaded java.util.SortedMap from shared objects file]
[Loaded java.util.NavigableMap from shared objects file]
[Loaded java.util.TreeMap from shared objects file]
[Loaded sun.misc.ASCIICaseInsensitiveComparator from shared objects file]
[Loaded java.util.TreeMap$Entry from shared objects file]
[Loaded sun.misc.VM from shared objects file]
[Loaded sun.nio.cs.ext.GBK from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar]
[Loaded java.lang.StringCoding from shared objects file]
[Loaded java.lang.ThreadLocal$ThreadLocalMap from shared objects file]
[Loaded java.lang.ThreadLocal$ThreadLocalMap$Entry from shared objects file]
[Loaded java.lang.StringCoding$StringDecoder from shared objects file]
[Loaded java.nio.charset.CharsetDecoder from shared objects file]
[Loaded sun.nio.cs.ext.DoubleByteDecoder from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar]
[Loaded sun.nio.cs.ext.GBK$Decoder from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar]
[Loaded java.nio.charset.CodingErrorAction from shared objects file]
[Loaded java.nio.ByteBuffer from shared objects file]
[Loaded java.nio.HeapByteBuffer from shared objects file]
[Loaded java.nio.Bits from shared objects file]
[Loaded java.nio.ByteOrder from shared objects file]
[Loaded java.lang.Readable from shared objects file]
[Loaded java.nio.CharBuffer from shared objects file]
[Loaded java.nio.HeapCharBuffer from shared objects file]
[Loaded java.nio.charset.CoderResult from shared objects file]
[Loaded java.nio.charset.CoderResult$Cache from shared objects file]
[Loaded java.nio.charset.CoderResult$1 from shared objects file]
[Loaded java.nio.charset.CoderResult$2 from shared objects file]
[Loaded sun.misc.Version from shared objects file]
[Loaded sun.misc.JavaLangAccess from shared objects file]
[Loaded java.lang.System$2 from shared objects file]
[Loaded sun.misc.SharedSecrets from shared objects file]
[Loaded java.lang.Runtime from shared objects file]
[Loaded java.io.File from shared objects file]
[Loaded java.io.FileSystem from shared objects file]
[Loaded java.io.Win32FileSystem from shared objects file]
[Loaded java.io.WinNTFileSystem from shared objects file]
[Loaded java.io.ExpiringCache from shared objects file]
[Loaded java.util.LinkedHashMap from shared objects file]
[Loaded java.io.ExpiringCache$1 from shared objects file]
[Loaded java.util.LinkedHashMap$Entry from shared objects file]
[Loaded sun.security.action.GetPropertyAction from shared objects file]
[Loaded sun.jkernel.DownloadManager from shared objects file]
[Loaded sun.jkernel.DownloadManager$1 from shared objects file]
[Loaded sun.jkernel.DownloadManager$2 from shared objects file]
[Loaded java.lang.ClassLoader$3 from shared objects file]
[Loaded java.io.ExpiringCache$Entry from shared objects file]
[Loaded java.lang.ClassLoader$NativeLibrary from shared objects file]
[Loaded java.lang.StringCoding$StringEncoder from shared objects file]
[Loaded java.nio.charset.CharsetEncoder from shared objects file]
[Loaded sun.nio.cs.ext.DoubleByteEncoder from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar]
[Loaded sun.nio.cs.ext.GBK$Encoder from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar]
[Loaded sun.nio.cs.Surrogate$Parser from shared objects file]
[Loaded sun.nio.cs.Surrogate from shared objects file]
[Loaded java.io.Closeable from shared objects file]
[Loaded java.io.InputStream from shared objects file]
[Loaded java.io.FileInputStream from shared objects file]
[Loaded java.io.FileDescriptor from shared objects file]
[Loaded java.io.Flushable from shared objects file]
[Loaded java.io.OutputStream from shared objects file]
[Loaded java.io.FileOutputStream from shared objects file]
[Loaded java.io.FilterInputStream from shared objects file]
[Loaded java.io.BufferedInputStream from shared objects file]
[Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater from shared objects file]
[Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl from shared objects file]
[Loaded sun.reflect.misc.ReflectUtil from shared objects file]
[Loaded java.io.FilterOutputStream from shared objects file]
[Loaded java.io.PrintStream from shared objects file]
[Loaded java.io.BufferedOutputStream from shared objects file]
[Loaded java.io.Writer from shared objects file]
[Loaded java.io.OutputStreamWriter from shared objects file]
[Loaded sun.nio.cs.StreamEncoder from shared objects file]
[Loaded java.io.BufferedWriter from shared objects file]
[Loaded java.lang.Terminator from shared objects file]
[Loaded sun.misc.SignalHandler from shared objects file]
[Loaded java.lang.Terminator$1 from shared objects file]
[Loaded sun.misc.Signal from shared objects file]
[Loaded sun.misc.NativeSignalHandler from shared objects file]
[Loaded sun.misc.OSEnvironment from shared objects file]
[Loaded sun.io.Win32ErrorMode from shared objects file]
[Loaded java.lang.NullPointerException from shared objects file]
[Loaded java.lang.ArithmeticException from shared objects file]
[Loaded java.lang.Compiler from shared objects file]
[Loaded java.lang.Compiler$1 from shared objects file]
[Loaded sun.misc.Launcher from shared objects file]
[Loaded java.net.URLStreamHandlerFactory from shared objects file]
[Loaded sun.misc.Launcher$Factory from shared objects file]
[Loaded java.security.SecureClassLoader from shared objects file]
[Loaded java.net.URLClassLoader from shared objects file]
[Loaded sun.misc.Launcher$ExtClassLoader from shared objects file]
[Loaded sun.security.util.Debug from shared objects file]
[Loaded java.lang.Package from shared objects file]
[Loaded java.net.URL from shared objects file]
[Loaded java.security.CodeSource from shared objects file]
[Loaded java.lang.Void from shared objects file]
[Loaded java.security.cert.Certificate from shared objects file]
[Loaded java.lang.ClassFormatError from shared objects file]
[Loaded java.lang.IllegalArgumentException from shared objects file]
[Loaded java.io.IOException from shared objects file]
[Loaded sun.misc.URLClassPath from shared objects file]
[Loaded java.lang.AssertionStatusDirectives from shared objects file]
[Loaded sun.misc.JavaNetAccess from shared objects file]
[Loaded java.net.URLClassLoader$7 from shared objects file]
[Loaded java.security.PermissionCollection from shared objects file]
[Loaded java.util.StringTokenizer from shared objects file]
[Loaded java.security.PrivilegedExceptionAction from shared objects file]
[Loaded sun.misc.Launcher$ExtClassLoader$1 from shared objects file]
[Loaded sun.misc.MetaIndex from shared objects file]
[Loaded java.io.Reader from shared objects file]
[Loaded java.io.BufferedReader from shared objects file]
[Loaded java.io.InputStreamReader from shared objects file]
[Loaded java.io.FileReader from shared objects file]
[Loaded sun.nio.cs.StreamDecoder from shared objects file]
[Loaded java.util.ArrayList from shared objects file]
[Loaded java.lang.reflect.Array from shared objects file]
[Loaded java.util.Locale from shared objects file]
[Loaded java.util.concurrent.ConcurrentMap from shared objects file]
[Loaded java.util.concurrent.ConcurrentHashMap from shared objects file]
[Loaded java.util.concurrent.locks.Lock from shared objects file]
[Loaded java.util.concurrent.locks.ReentrantLock from shared objects file]
[Loaded java.util.concurrent.ConcurrentHashMap$Segment from shared objects file]
[Loaded java.util.concurrent.locks.AbstractOwnableSynchronizer from shared objects file]
[Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer from shared objects file]
[Loaded java.util.concurrent.locks.ReentrantLock$Sync from shared objects file]
[Loaded java.util.concurrent.locks.ReentrantLock$NonfairSync from shared objects file]
[Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer$Node from shared objects file]
[Loaded java.util.concurrent.ConcurrentHashMap$HashEntry from shared objects file]
[Loaded java.lang.CharacterDataLatin1 from shared objects file]
[Loaded java.io.ObjectStreamClass from shared objects file]
[Loaded sun.net.www.ParseUtil from shared objects file]
[Loaded java.util.BitSet from shared objects file]
[Loaded java.net.Parts from shared objects file]
[Loaded java.net.URLStreamHandler from shared objects file]
[Loaded sun.net.www.protocol.file.Handler from shared objects file]
[Loaded java.util.HashSet from shared objects file]
[Loaded sun.net.www.protocol.jar.Handler from shared objects file]
[Loaded sun.misc.Launcher$AppClassLoader from shared objects file]
[Loaded sun.misc.Launcher$AppClassLoader$1 from shared objects file]
[Loaded java.lang.SystemClassLoaderAction from shared objects file]
[Loaded java.net.URLClassLoader$1 from shared objects file]
[Loaded sun.net.util.URLUtil from shared objects file]
[Loaded sun.misc.URLClassPath$3 from shared objects file]
[Loaded sun.misc.URLClassPath$Loader from shared objects file]
[Loaded sun.misc.URLClassPath$JarLoader from shared objects file]
[Loaded java.security.PrivilegedActionException from shared objects file]
[Loaded sun.misc.URLClassPath$FileLoader from shared objects file]
[Loaded sun.misc.Resource from shared objects file]
[Loaded sun.misc.URLClassPath$FileLoader$1 from shared objects file]
[Loaded sun.nio.ByteBuffered from shared objects file]
[Loaded java.security.Permissions from shared objects file]
[Loaded java.net.URLConnection from shared objects file]
[Loaded sun.net.www.URLConnection from shared objects file]
[Loaded sun.net.www.protocol.file.FileURLConnection from shared objects file]
[Loaded java.net.ContentHandler from shared objects file]
[Loaded java.net.UnknownContentHandler from shared objects file]
[Loaded sun.net.www.MessageHeader from shared objects file]
[Loaded java.io.FilePermission from shared objects file]
[Loaded java.io.FilePermission$1 from shared objects file]
[Loaded java.security.Policy from shared objects file]
[Loaded sun.security.provider.PolicyFile from shared objects file]
[Loaded java.security.Policy$UnsupportedEmptyCollection from shared objects file]
[Loaded java.io.FilePermissionCollection from shared objects file]
[Loaded java.security.AllPermission from shared objects file]
[Loaded java.security.UnresolvedPermission from shared objects file]
[Loaded java.security.BasicPermissionCollection from shared objects file]
[Loaded sun.misc.JavaSecurityProtectionDomainAccess from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar]
[Loaded java.security.ProtectionDomain$2 from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar]
[Loaded java.security.ProtectionDomain$Key from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar]
[Loaded java.security.Principal from shared objects file]
[Loaded ClassLoaderTest from file:/F:/Desktop/]
[Loaded ClassA from file:/F:/Desktop/]
Loading ClassA
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]

 

由上可以看到,jvm将常用类加载到系统,这样可以加快运行的效率。

 

这引出了加载类的两种类型:

 

  • 预先加载( pre-loading ) 可以加快运行效率,和io损耗
  • 按需加载( load-on-demand ) 可以减少内存消耗
系统常用类采用预加载方式加载,而用户自定义的类就采用按需加载的方式。而加载一个类进入内存的时机便是调用构造函数时。

从加载形式来看,又分为:
  • 隐式加载( implicit )程序中用 new 关键字来定义一个实例变量, JRE 在执行到 new 关键字的时候就会把对应的实例类加载进入内存
  • 显示加载( explicit )利用java提供的加载方法,通过代码方式显示的加载class至内存

 

显示加载的例子:

 

class TestClass{
	public void method(){
     System.out.println("TestClass-method");
	}
}
 
public class CLTest {
	public static void main(String args[]) {
     try{
        Class c = Class.forName("TestClass");
        TestClass object = (TestClass)c.newInstance();
        object.method();
      }catch(Exception e){
        e.printStackTrace();
     }
	}
}

     我们通过 Class 类的 forName (String s) 方法把自定义类 TestClass 加载进来,并通过 newInstance ()方法把实例初始化。

Class 的 forName() 方法还有另外一种形式:
Class forName(String s, boolean flag, ClassLoader classloader), s 表示需要加载类的名称, flag 表示在调用该函数加载类的时候是否初始化静态区, classloader 表示加载该类所需的加载器。
forName (String s) 是默认通过 ClassLoader.getCallerClassLoader() 调用类加载器的,但是该方法是私有方法,我们无法调用,如果我们想使用 Class forName(String s, boolean flag, ClassLoader classloader) 来加载类的话,就必须要指定类加载器,可以通过如下的方式来实现:
Test test = new Test();//Test 类为自定义的一个测试类;
ClassLoader cl = test. getClass().getClassLoader(); // 获取 test 的类装载器;
Class c = Class.forName("TestClass", true, cl);
因为一个类要加载就必需要有加载器,这里我们是通过获取加载 Test 类的加载器 cl 当作加载 TestClass 的类加载器来实现加载的。

    这些class必须在classpath下面。


如果不在classpath下,还想要加载class进来,就需要用到class加载的另一种方式。

  • 自定义加载机制
  try{
         URL url = new URL("file:/d:/test/lib/");
         URLClassLoader urlCL = new URLClassLoader(new URL[]{url});
         Class c = urlCL.loadClass("TestClassA");
         TestClassA object = (TestClassA)c.newInstance();
          object.method();
   }catch(Exception e){
          e.printStackTrace();
     }
 
类加载器的体系结构



 
这里有一个加载的顺序问题,也就是类加载器的代理机制。

类加载器在尝试自己去查找某个类的字节代码并定义它时,会先代理给其父类加载器,由父类加载器先去尝试加载这个类,依次类推。在介绍代理模式背后的动机之前,首先需要说明一下 Java 虚拟机是如何判定两个 Java 类是相同的。Java 虚拟机不仅要看类的全名是否相同,还要看加载此类的类加载器是否一样。只有两者都相同的情况,才认为两个类是相同的。即便是同样的字节代码,被不同的类加载器加载之后所得到的类,也是不同的。试图对这两个类的对象进行相互赋值,会抛出运行时异常 ClassCastException

当执行 java ***.class 的时候, java.exe 会帮助我们找到 JRE ,接着找到位于 JRE 内部的 jvm.dll ,这才是真正的 Java 虚拟机器 , 最后加载动态库,激活 Java 虚拟机器。虚拟机器激活以后,会先做一些初始化的动作,比如说读取系统参数等。一旦初始化动作完成之后,就会产生第一个类加载器―― Bootstrap Loader , Bootstrap Loader 是由 C++ 所撰写而成,这个 Bootstrap Loader 所做的初始工作中,除了一些基本的初始化动作之外,最重要的就是加载 Launcher.java 之中的 ExtClassLoader ,并设定其 Parent 为 null ,代表其父加载器为 BootstrapLoader 。然后Bootstrap Loader 再要求加载 Launcher.java 之中的 AppClassLoader ,并设定其 Parent 为之前产生的 ExtClassLoader 实体。这两个加载器都是以静态类的形式存在的。这里要请大家注意的是, Launcher$ExtClassLoader.class 与 Launcher$AppClassLoader.class 都是由 Bootstrap Loader 所加载,所以 Parent 和由哪个类加载器加载没有关系。

这三个加载器就构成我们的 Java 类加载体系。他们分别从以下的路径寻找程序所需要的类:
BootstrapLoader : sun.boot.class.path
ExtClassLoader:      java.ext.dirs
AppClassLoader:      java.class.path
这三个系统参量可以通过 System.getProperty() 函数得到具体对应的路径。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值