JVM 加载.class的过程

JVM 加载.class的过程

FROM : http://rq2-79.iteye.com/blog/1393675

FROM : http://www.cnblogs.com/focusj/archive/2012/03/05/2375357.html

JVM要调用一个类里的方法需要经过以下几个步骤:类装载、链接、初始化。

1 类装载:

JVM通过类得全限定名(包命+类名)找到类的.class文件。然后把这个.class文件加载进来,这个过程需要通过ClassLoader来实现。JVM的类加载系统结构如下(:1-1)

 

Boostrap ClassLoader:启动类加载器,它用来加载一些jdk的核心类,主要负责JAVA_HOME
/jre/lib下的类的加载,可以通过参数-Xbootclasspath制定需要装入的jar包。 它本身不是用java实现的,所以肯定不是ClassLoader的子类了。

Extendsion ClassLoader:扩展类加载器,用来加载一些扩展类,主要负责JAVA_HOME
/jre/lib/ext下类的加载。此类是ClassLoader的一个子类。

System ClassLoader:系统类加载器 也叫Application ClassLoader。是离我们最近的ClassLoader了,它负责加载CLASSPATH里指定的那些类。我们要实现自己的ClassLoader也是继承自该类。SystemClassLoader的父类是Extension ClassLoader

类的加载过程分两步:第一步:从下往上查找类是否已经加载,如果找到,直接返回已加载的类,如果没找着接着往上找。第二步:如果到Bootstrap ClassLoader还没找到,这时Bootstrap ClassLoader会尝试加载该类,如果成功加载,直接返回加载后的类,如果无法加载,交由Extension ClassLoader去加载,依次类推。如果最后仍然没找到,程序会抛出ClassNotFoundException.

2 链接:

当一个class文件被成功加载后,接下来就要做链接了。链接就是要把二进制的.class文件转换成可以被jvm执行的Class对象的过程。这个过程又分为:检验、准备、解析。

检验:就是检查.class的结构是否正确,是否符合Java虚拟机的语义要求。

准备:包括创建类或接口的静态域以及把这些静态域初始化为标准的缺省值。注意此处的初始化不同于后面的的初始化步骤。如有一个staticString 变量str,我们知道,在JAVAString变量默认的初始值是null,此处的初始化就是将null赋值给str

解析:将类中对另一个类或接口的符合引号转化成全限定名引用,将对他们的方法、字段的符合引用转化成直接引用。

3 初始化:执行类或接口中的静态初始化函数(块),将静态变量初始化。这就是我们平时理解的对静态变量赋值。

至此,一个类才加载完成,可以调用类的类变量了(静态变量)和对类进行实例化了。

前面发了几篇学习笔记,但是看这些东西总是感觉很"玄乎",来一篇实战的东西来揭一下"JVM"的面纱,让"SSH"时代的童鞋们来熟悉一下Java的"老祖爷"JVM。由于自己的水平有限,所以大家在看过程中发了什么问题,或者您有什么疑问请及时提出来,我及时解决。如果您有什么建议,那么更好大家一块讨论。

1、源码文件

 
 
  1. public class LearningClassFile {  
  2.     //普通变量  
  3.     private int id1;  
  4.     //静态变量  
  5.     private static int id2;  
  6.     //常量  
  7.     private final int id3 = 4;  
  8.     //静态常量  
  9.     private static final int id4 = 5;  
  10.            
  11.     public LearningClassFile() {  
  12.     }  
  13.        
  14.     public LearningClassFile(int id1, int id2) {  
  15.         this.id1 = id1;  
  16.         this.id2 = id2;  
  17.     }  
  18.        
  19.     //使用public修饰的addPub方法  
  20.     public void addPub(int a, int b) {  
  21.         int result = a + b;  
  22.         System.out.println(result);  
  23.     }  
  24.        
  25.     //使用private修饰的addPri方法  
  26.     private void addPri(int a, int b) {  
  27.         int result = a + b;  
  28.         System.out.println(result);  
  29.     }  
  30.        
  31.     //使用static修饰的方法  
  32.     public static void addSta() {  
  33.         int result = id2 + id4;  
  34.         System.out.println(result);  
  35.     }  
  36.        
  37.     public static final void addFinal(int a, int b) {  
  38.         int result = a + b;  
  39.         System.out.println(result);  
  40.     }  
  41.        
  42.     public static void main(String[] args) {  
  43.         LearningClassFile lcf = new LearningClassFile(12);  
  44.         lcf.addPub(12);  
  45.         lcf.addPri(12);  
  46.         addSta();  
  47.         addFinal(12);  
  48.     }  

Class文件:

 
 
  1. Compiled from "LearningClassFile.java" 
  2. public class LearningClassFile extends java.lang.Object  
  3.   SourceFile: "LearningClassFile.java" 
  4.   minor version: 0 
  5.   major version: 50 
  6. //运行时常量池:用于存放编译期生成的各种字面量和符号引用。  
  7.   Constant pool:  
  8. //从父类Object继承的默认构造方法  
  9. //观察该方法的特征:无参,返回类型void  
  10. const #1 = Method       #13.#35;        //  java/lang/Object."<init>":()V  
  11. //常量id3  
  12. //"#7.#36; //  LearningClassFile.id3:I"  
  13. //#7:查找常量池中的类名LearningClassFile  
  14. //#36-->"const #36 = NameAndType #17:#15;//  id3:I"  
  15. //NameAndType字面的意思是名称和类型。即id3是变量的名称,I表示id3是int类型  
  16. //综合描述:LearningClassFile中的id3是int类型  
  17. const #2 = Field        #7.#36//  LearningClassFile.id3:I  
  18. const #3 = Field        #7.#37//  LearningClassFile.id1:I  
  19. const #4 = Field        #7.#38//  LearningClassFile.id2:I  
  20. //将System的out存储至常量池  
  21. //System类中out被public static final修饰的  
  22. //"public final static PrintStream out = nullPrintStream();"  
  23. //综合描述:System类的out属性是PrintStream类型  
  24. const #5 = Field        #39.#40;        //  java/lang/System.out:Ljava/io/PrintS  
  25. tream;  
  26. //将PrintStream的Println()方法存储至常量池  
  27. //该方法的参数为I,返回值为void  
  28. const #6 = Method       #41.#42;        //  java/io/PrintStream.println:(I)V  
  29. //类LearningClassFIle  
  30. const #7 = class        #43;    //  LearningClassFile  
  31. //构造函数  
  32. //该构造函数需传入两个int类型的变量  
  33. const #8 = Method       #7.#44//  LearningClassFile."<init>":(II)V  
  34. //LearningClassFile的addPub方法  
  35. //#4-->"const #45 = NameAndType #27:#26;//  addPub:(II)V"  
  36. //#27-->"const #27 = Asciz       addPub;"    方法的名称为:addPub  
  37. //#26-->"const #26 = Asciz       (II)V;"     方法的类型:两个int类型的参数,返回类型为void  
  38. const #9 = Method       #7.#45//  LearningClassFile.addPub:(II)V  
  39. const #10 = Method      #7.#46//  LearningClassFile.addPri:(II)V  
  40. const #11 = Method      #7.#47//  LearningClassFile.addSta:()V  
  41. const #12 = Method      #7.#48//  LearningClassFile.addFinal:(II)V  
  42. const #13 = class       #49;    //  java/lang/Object  
  43. const #14 = Asciz       id1;  
  44. const #15 = Asciz       I;  
  45. const #16 = Asciz       id2;  
  46. const #17 = Asciz       id3;  
  47. //ConstantValue属性表示一个常量字段的值  
  48. //即final修饰的属性  
  49. const #18 = Asciz       ConstantValue;  
  50. //对于final修饰的常量直接将类型和值存入常量池  
  51. const #19 = int 4;  
  52. const #20 = Asciz       id4;  
  53. const #21 = int 5;  
  54. const #22 = Asciz       <init>;  
  55. const #23 = Asciz       ()V;  
  56. //Code属性只为唯一一个方法、实例类初始化方法或类初始化方法保存Java虚拟机指令及相关辅助信息  
  57. //简而言之:保存方法编译后的指令信息  
  58. const #24 = Asciz       Code;  
  59. //java源码行号与编译后的字节码指令的对应表  
  60. const #25 = Asciz       LineNumberTable;  
  61. const #26 = Asciz       (II)V;  
  62. const #27 = Asciz       addPub;  
  63. const #28 = Asciz       addPri;  
  64. const #29 = Asciz       addSta;  
  65. const #30 = Asciz       addFinal;  
  66. const #31 = Asciz       main;  
  67. const #32 = Asciz       ([Ljava/lang/String;)V;  
  68. //java 源码文件  
  69. const #33 = Asciz       SourceFile;  
  70. const #34 = Asciz       LearningClassFile.java;  
  71. const #35 = NameAndType #22:#23;//  "<init>":()V  
  72. const #36 = NameAndType #17:#15;//  id3:I  
  73. const #37 = NameAndType #14:#15;//  id1:I  
  74. const #38 = NameAndType #16:#15;//  id2:I  
  75. const #39 = class       #50;    //  java/lang/System  
  76. const #40 = NameAndType #51:#52;//  out:Ljava/io/PrintStream;  
  77. const #41 = class       #53;    //  java/io/PrintStream  
  78. const #42 = NameAndType #54:#55;//  println:(I)V  
  79. const #43 = Asciz       LearningClassFile;  
  80. const #44 = NameAndType #22:#26;//  "<init>":(II)V  
  81. const #45 = NameAndType #27:#26;//  addPub:(II)V  
  82. const #46 = NameAndType #28:#26;//  addPri:(II)V  
  83. const #47 = NameAndType #29:#23;//  addSta:()V  
  84. const #48 = NameAndType #30:#26;//  addFinal:(II)V  
  85. const #49 = Asciz       java/lang/Object;  
  86. const #50 = Asciz       java/lang/System;  
  87. const #51 = Asciz       out;  
  88. const #52 = Asciz       Ljava/io/PrintStream;;  
  89. const #53 = Asciz       java/io/PrintStream;  
  90. const #54 = Asciz       println;  
  91. const #55 = Asciz       (I)V;  
  92.    
  93. {  
  94. //默认构造方法  
  95. public LearningClassFile();  
  96.   Code:  
  97.    Stack=2, Locals=1, Args_size=1 
  98.    0:   aload_0      
  99.    1:   invokespecial   #1//Method java/lang/Object."<init>":()V  
  100.    //将id3的引用推送至栈顶  
  101.    4:   aload_0  
  102.    //将4推送至栈顶  
  103.    5:   iconst_4  
  104.    //将4赋值给id3  
  105.    6:   putfield        #2//Field id3:I  
  106.    9:   return 
  107.   LineNumberTable:  
  108.    line 110   //public LearningClassFile() {  
  109.                 //对于final类型的实例变量在每个构造方法中都会进行一次初始化。  
  110.    line 74    //    private final int id3 = 4;   
  111.    line 129   //}  
  112.    
  113.    
  114. public LearningClassFile(intint);  
  115.   Code:  
  116.    Stack=2, Locals=3, Args_size=3 
  117.    0:   aload_0  
  118.    1:   invokespecial   #1//Method java/lang/Object."<init>":()V  
  119.    4:   aload_0  
  120.    5:   iconst_4  
  121.    6:   putfield        #2//Field id3:I  
  122.    9:   aload_0  
  123.    10:  iload_1  
  124.    11:  putfield        #3//Field id1:I  
  125.    14:  aload_0  
  126.    15:  pop  
  127.    16:  iload_2  
  128.    17:  putstatic       #4//Field id2:I  
  129.    20:  return 
  130.   LineNumberTable:  
  131.    line 140    //public LearningClassFile(int id1, int id2) {  
  132.                  //对于final类型的实例变量在每个构造方法中都会进行一次初始化。  
  133.    line 74     //    private final int id3 = 4;     
  134.    line 159    //    this.id1 = id1;  
  135.    line 1614   //    this.id2 = id2;  
  136.    line 1720   //}  
  137.    
  138.    
  139. public void addPub(intint);  
  140.   Code:  
  141.    Stack=2, Locals=4, Args_size=3 
  142.    0:   iload_1  
  143.    1:   iload_2  
  144.    2:   iadd  
  145.    3:   istore_3  
  146.    4:   getstatic       #5//Field java/lang/System.out:Ljava/io/PrintStream;  
  147.    7:   iload_3  
  148.    8:   invokevirtual   #6//Method java/io/PrintStream.println:(I)V  
  149.    11:  return 
  150.   LineNumberTable:  
  151.    line 210    //    int result = a + b;   
  152.    line 224    //    System.out.println(result);  
  153.    line 2311   // }  
  154.    
  155.    
  156. public static void addSta();  
  157.   Code:  
  158.    Stack=2, Locals=1, Args_size=0 
  159.    //获取静态变量id2推送至栈顶  
  160.    0:   getstatic       #4//Field id2:I  
  161.    //直接从常量池中取出id4的值5推送至栈顶  
  162.    3:   iconst_5  
  163.    //执行相加操作  
  164.    4:   iadd  
  165.    //将计算结果推送至栈顶  
  166.    5:   istore_0  
  167.    //获取静态与out  
  168.    6:   getstatic       #5//Field java/lang/System.out:Ljava/io/PrintStream;  
  169.    //取出计算结果  
  170.    9:   iload_0  
  171.    //调用println方法  
  172.    10:  invokevirtual   #6//Method java/io/PrintStream.println:(I)V  
  173.    //方法正常结束  
  174.    13:  return 
  175.   LineNumberTable:  
  176.    line 330    //     int result = id2 + id4;  
  177.    line 346    //     System.out.println(result);  
  178.    line 3513   //}  
  179.    
  180.    
  181. public static final void addFinal(intint);  
  182.   Code:  
  183.    Stack=2, Locals=3, Args_size=2 
  184.    0:   iload_0  
  185.    1:   iload_1  
  186.    2:   iadd  
  187.    3:   istore_2  
  188.    4:   getstatic       #5//Field java/lang/System.out:Ljava/io/PrintStream;  
  189.    7:   iload_2  
  190.    8:   invokevirtual   #6//Method java/io/PrintStream.println:(I)V  
  191.    11:  return 
  192.   LineNumberTable:  
  193.    line 380 
  194.    line 394 
  195.    line 4011 
  196.    
  197.    
  198. public static void main(java.lang.String[]);  
  199.   Code:  
  200.    Stack=4, Locals=2, Args_size=1 
  201.    //创建一个LearningClassFile对象,并将对象的引用推送至栈顶  
  202.    0:   new     #7//class LearningClassFile  
  203.    //将对象的引用进行备份推送至栈顶  
  204.    //使用原有的引用值调用实例方法,现在置于栈顶的引用值的位置将被接下来的操作覆盖。  
  205.    3:   dup  
  206.    //将构造函数中的参数1推送至栈顶  
  207.    4:   iconst_1  
  208.    5:   iconst_2  
  209.    //执行构造方法  
  210.    6:   invokespecial   #8//Method "<init>":(II)V  
  211.    //将栈顶引用型数值存入第二个本地变量  
  212.    9:   astore_1  
  213.    10:  aload_1  
  214.    11:  iconst_1  
  215.    12:  iconst_2  
  216.    //调用实例方法  
  217.    13:  invokevirtual   #9//Method addPub:(II)V  
  218.    16:  aload_1  
  219.    17:  iconst_1  
  220.    18:  iconst_2  
  221.    19:  invokespecial   #10//Method addPri:(II)V  
  222.    //调用静态方法  
  223.    22:  invokestatic    #11//Method addSta:()V  
  224.    25:  iconst_1  
  225.    26:  iconst_2  
  226.    27:  invokestatic    #12//Method addFinal:(II)V  
  227.    30:  return 
  228.   LineNumberTable:  
  229.    line 430     //   LearningClassFile lcf = new LearningClassFile(1, 2);  
  230.    line 4410    //   lcf.addPub(1, 2);  
  231.    line 4516    //   lcf.addPri(1, 2);  
  232.    line 4622    //   addSta();  
  233.    line 4725    //   addFinal(1, 2);  
  234.    line 4830    //}  
  235. }  

final变量和static final变量的区别:

(1)实例常量和类常量的区别

(2)初识方式不同:从class字节码来看final修饰的变量会出现在每个构造方法中进行一次初始化;static final类型的变量必须在定义的时候进行初始化。 理解"编译期可知,运行期不变": 编译器可确定调用方法的版本,符合这个标准的方法主要有两种:私有方法,静态方法。

详情请看:深入理解JVM读书笔记--字节码执行引擎。

2、final变量和static final变量的区别:

(1)实例常量和类常量的区别

(2)初始化方式不同:从class字节码来看final修饰的变量会出现在每个构造方法中进行一次初始化;static final类型的变量必须在定义的时候进行初始化。

3、理解"编译期可知,运行期不变":

编译器可确定调用方法的版本,符合这个标准的方法主要有两种:私有方法,静态方法。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值