本帖最后由 隋营营 于 2012-5-6 13:59 编辑
代码最能说明问题:
public class Testext {
public static void main(String[] args) throws Exception {
Class.forName("java.util.Date");
}
}
在Eclipse的Run Configurations中设置VM arguments:-verbose:class
运行程序输出如下:(很长……有点耐心,关键在末尾的输出语句!)
[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.StringBuilder from shared objects file]
[Loaded java.lang.StackTraceElement from shared objects file]
[Loaded java.nio.Buffer from shared objects file]
[Opened D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.PostVMInitHook 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.util.concurrent.atomic.AtomicInteger from shared objects file]
[Loaded java.lang.reflect.ReflectAccess from shared objects file]
[Loaded java.util.Arrays from shared objects file]
[Loaded java.nio.charset.Charset$3 from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Opened D:\Soft\Program\JAVA\JDK\jre\lib\charsets.jar]
[Loaded sun.nio.cs.AbstractCharsetProvider from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.nio.cs.ext.ExtendedCharsets from D:\Soft\Program\JAVA\JDK\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 D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.VM from shared objects file]
………………
[Loaded java.io.FilePermission from shared objects file]
[Loaded java.io.FilePermission$1 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 sttt.Testext from file:/D:/DTemp/JAVAWorkspace/suiusoar/bin/] 这是我自己写的类
[Loaded java.lang.Void from shared objects file]
[Loaded java.util.Date from shared objects file] java.util.Date在这里被加载到内存
[Loaded sun.util.calendar.CalendarSystem from shared objects file]
[Loaded sun.util.calendar.AbstractCalendar from shared objects file]
[Loaded sun.util.calendar.BaseCalendar from shared objects file]
[Loaded sun.util.calendar.Gregorian from shared objects file]
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]
看到了吗?其中有我们非常熟悉的类
这里涉及到了JAVA的ClassLoader的运行机制:
bootstrapclass loader:装载JDK最核心的类(如java.lang包中的类)以及其它的ClassLoader,其它的 ClassLoader再装载其它类
ExtClassLoader:装载目录jre\lib\ext目录下的类
AppClassLoader:装载我们自己写的类(这里是sttt.Testext )
JDK在加载类时首先看这个类是否被上一层的ClassLoader加载过了,若已经加载,不会重新加载!
|