java jvm 字节码 实例

https://blog.csdn.net/wuzhiwei549/article/details/80626677

代码

package strings;
//: strings/WhitherStringBuilder.java

public class WhitherStringBuilder {
  public String implicit(String[] fields) {
    String result = "";
    for(int i = 0; i < fields.length; i++)
      result += fields[i];
    return result;
  }
  public String explicit(String[] fields) {
    StringBuilder result = new StringBuilder();
    for(int i = 0; i < fields.length; i++)
      result.append(fields[i]);
    return result.toString();
  }
} ///:~

生成的字节码如下

public class strings.WhitherStringBuilder { 
  public strings.WhitherStringBuilder();
    Code:
       0: aload_0    // 将this引用推送至栈顶,即压入栈                
       1: invokespecial #8 //调用<init>方法实例化对象// Method java/lang/Object."<init>":()V
       4: return

  public java.lang.String implicit(java.lang.String[]);
    Code:
       0: ldc           #16 //将编号为#16的字符串推送至栈顶 // String
       2: astore_2     //将栈顶引用类型值存入第三个本地变量
       3: iconst_0      //将int型0推送至栈顶
       4: istore_3      //将栈顶int型数值存入第四个本地变量
       5: goto          32  //无条件跳转到32行
       8: new           #18  //新建一个StringBuilder对象编号#18,并将其引用压入栈顶// class java/lang/StringBuilder
      11: dup           //复制栈顶数值(在这里时引用)并将复制数值压入栈顶
      12: aload_2      //将第三个本地引用推送至栈顶
      13: invokestatic  #20//调用静态方法(这里调用了valueOf())// Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
      16: invokespecial #26//调用超类构造方法,实例初始化方法,私有方法 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
      19: aload_1       //将第二个引用本地变量(即数组引用)压入栈顶
      20: iload_3       //将第四个int本地变量压入栈顶
      21: aaload        //将引用数组指定索引的值推送至栈顶(即二个本地引用变量所代表的数组的下标为第四个int本地变量的值)
      22: invokevirtual #29//调用实例化方法append// Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      25: invokevirtual #33 //调用实例化方法toString将结果转换为String// Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      28: astore_2          //将栈顶引用类型值存入第三个本地变量
      29: iinc          3, 1 //将第4个int本地变量加1
      32: iload_3          //将第4个int本地变量推至栈顶
      33: aload_1          //将第二个引用类型本地办理推送至栈顶,第二个引用应该是改方法的参数的数组的引用
      34: arraylength      //获得数组的长度并压入栈顶 
      35: if_icmplt     8  //比较栈顶两int数值的大小如果小于0跳转到第8行
      38: aload_2          //将第二个本地引用类型的本地变量推送至栈顶
      39: areturn          //返回栈顶引用型本地变量,并退出方法

  public java.lang.String explicit(java.lang.String[]);
    Code:
       0: new           #18                 // class java/lang/StringBuilder
       3: dup
       4: invokespecial #45                 // Method java/lang/StringBuilder."<init>":()V
       7: astore_2
       8: iconst_0
       9: istore_3
      10: goto          24
      13: aload_2
      14: aload_1
      15: iload_3
      16: aaload
      17: invokevirtual #29                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      20: pop
      21: iinc          3, 1
      24: iload_3
      25: aload_1
      26: arraylength
      27: if_icmplt     13
      30: aload_2
      31: invokevirtual #33                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      34: areturn
}

 

posted @ 2019-01-23 12:57  江期玉  阅读(302)  评论(0编辑  收藏  举报