Java 字节码

简单加法字节码

程序代码

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = a + b;
        System.out.println(c);
    }
}

编译Test.java

javac -g:vars Test.java

使用-g:vars会生成本地变量表信息

Test.class转换成字节码输出到控制台

javap -v -l Test.class

或者输出到文件中

javap -v -l Test.class > test.txt

字节码文件内容如下所示

Classfile /D:/LZC/code/example/javacode/demo/src/main/java/Test.class
  Last modified 2020-4-23; size 457 bytes
  MD5 checksum 8afe0a5acd5640f0053847a86b66d084
public class Test
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #5.#20         // java/lang/Object."<init>":()V
   #2 = Fieldref           #21.#22        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = Methodref          #23.#24        // java/io/PrintStream.println:(I)V
   #4 = Class              #25            // Test
   #5 = Class              #26            // java/lang/Object
   #6 = Utf8               <init>
   #7 = Utf8               ()V
   #8 = Utf8               Code
   #9 = Utf8               LocalVariableTable
  #10 = Utf8               this
  #11 = Utf8               LTest;
  #12 = Utf8               main
  #13 = Utf8               ([Ljava/lang/String;)V
  #14 = Utf8               args
  #15 = Utf8               [Ljava/lang/String;
  #16 = Utf8               a
  #17 = Utf8               I
  #18 = Utf8               b
  #19 = Utf8               c
  #20 = NameAndType        #6:#7          // "<init>":()V
  #21 = Class              #27            // java/lang/System
  #22 = NameAndType        #28:#29        // out:Ljava/io/PrintStream;
  #23 = Class              #30            // java/io/PrintStream
  #24 = NameAndType        #31:#32        // println:(I)V
  #25 = Utf8               Test
  #26 = Utf8               java/lang/Object
  #27 = Utf8               java/lang/System
  #28 = Utf8               out
  #29 = Utf8               Ljava/io/PrintStream;
  #30 = Utf8               java/io/PrintStream
  #31 = Utf8               println
  #32 = Utf8               (I)V
{
  public Test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   LTest;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=4, args_size=1
         0: bipush        10 
         2: istore_1
         3: bipush        20
         5: istore_2
         6: iload_1
         7: iload_2
         8: iadd
         9: istore_3
        10: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
        13: iload_3
        14: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
        17: return
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      18     0  args   [Ljava/lang/String;
            3      15     1     a   I
            6      12     2     b   I
           10       8     3     c   I
}

LocalVariableTable描述了本地变量表,可以看出,程序中共有四个本地变量,Slot代表变量索引位置,Name代表变量名字。

分析字节码执行过程

// 将10入栈
0: bipush        10 
// 出栈,并将值存储到索引为1的本地变量中
2: istore_1
// 将20入栈
3: bipush        20
// 出栈,并将值存储到索引为2的本地变量中
5: istore_2
// 将索引为1的变量值入栈
6: iload_1
// 将索引为2的变量值入栈
7: iload_2
// 执行int类型的加法,将10和20出栈并将这两个值相加,最后把相加结果如栈,即将30入栈
8: iadd
// 出栈,并将值存储到索引为3的本地变量中
9: istore_3
// 将PrintStream入栈
10: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
// 将索引为3的变量值入栈
13: iload_3
// 出栈,出栈。执行PrintStream.println方法
14: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
17: return

i++与++i字节码区别

i++字节码

代码

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = a++;
        System.out.println(b);
    }
}

字节码

// 将10入栈
0: bipush        10
// 出栈,将10存储到索引为1的本地变量中,即 a = 10;
2: istore_1
// 将索引为1的本地变量值入栈
3: iload_1
// iinc indexbyte,constbyte
// 将整数值constbyte加到indexbyte指定的int类型的局部变量中。
// 将本地变量a的值加1,此时a=11,但此时栈里面的值仍然为10
4: iinc          1, 1
// 出栈(此时栈顶元素为10),将值存储到索引为2的本地变量中,即 b = 10
7: istore_2
8: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
11: iload_2
12: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
15: return

// 本地变量表
LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      16     0  args   [Ljava/lang/String;
            3      13     1     a   I
            8       8     2     b   I

++i字节码

代码

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = ++a;
        System.out.println(b);
    }
}

字节码

// 将10入栈
0: bipush        10
// 出栈,将10存储到索引为1的本地变量中,即 a = 10;
2: istore_1
// iinc indexbyte,constbyte
// 将整数值constbyte加到indexbyte指定的int类型的局部变量中。
// 将本地变量a的值加1,此时a=11
3: iinc          1, 1
// 将索引为1的本地变量值入栈,即将11入栈
6: iload_1
// 出栈(此时栈顶元素为11),将值存储到索引为2的本地变量中,即 b = 11
7: istore_2
8: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
11: iload_2
12: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
15: return
 LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      16     0  args   [Ljava/lang/String;
            3      13     1     a   I
            8       8     2     b   I

区别

i++是先将本地变量值入栈,然后将本地变量值加1;

++i是先将本地变量值加1,然后再将本地变量入栈。

字符串常量

代码

/**
 * Java环境为JDK8,如果是JDK6,打印结果会不一样
 * JDK6时,常量池放在永久代
 * JDK7开始,常量池放在堆上
 * JDK8开始,元空间取代了永久代
 */
public class Test {
    public static void main(String[] args) {
        // 在堆里面创建一个"11"对象。此时常量池里面没有"11",所以在常量池里面也创建"11"
        String a1 = new String("11");
        // 此时常量池里面已经有了"11"了,这里直接返回常量池里面的引用
        a1.intern();
        // 将b1指向常量池里面的"11"
        String b1 = "11";
        // a1指向堆里面的地址,b1指向常量池里面的地址,所以这里为false
        System.out.println(a1 == b1); // false

        // 这里会在堆里面创建"aa",在常量池里面创建"a"
        String a2 = new String("a") + new String("a");
        // 常量池里面没有"aa",所以讲这个对象添加到常量池中
        // 这里并不是直接在常量池里面新建一个"aa"对象
        // 而是生成一个指向堆里面"aa"对象的一个引用
        a2.intern();
        // 将b2指向常量池里面的"aa"
        String b2 = "aa";
        System.out.println(a2 == b2); // true
    }
}

字节码

Classfile /D:/LZC/code/example/javacode/demo/src/main/java/Test.class
  Last modified 2020-4-23; size 936 bytes
  MD5 checksum 807cfb9720864177ebe7f056d1031b6b
public class Test
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #15.#35        // java/lang/Object."<init>":()V
   #2 = Class              #36            // java/lang/String
   #3 = String             #37            // 11
   #4 = Methodref          #2.#38         // java/lang/String."<init>":(Ljava/lang/String;)V
   #5 = Methodref          #2.#39         // java/lang/String.intern:()Ljava/lang/String;
   #6 = Fieldref           #40.#41        // java/lang/System.out:Ljava/io/PrintStream;
   #7 = Methodref          #42.#43        // java/io/PrintStream.println:(Z)V
   #8 = Class              #44            // java/lang/StringBuilder
   #9 = Methodref          #8.#35         // java/lang/StringBuilder."<init>":()V
  #10 = String             #45            // a
  #11 = Methodref          #8.#46         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #12 = Methodref          #8.#47         // java/lang/StringBuilder.toString:()Ljava/lang/String;
  #13 = String             #48            // aa
  #14 = Class              #49            // Test
  #15 = Class              #50            // java/lang/Object
  #16 = Utf8               <init>
  #17 = Utf8               ()V
  #18 = Utf8               Code
  #19 = Utf8               LocalVariableTable
  #20 = Utf8               this
  #21 = Utf8               LTest;
  #22 = Utf8               main
  #23 = Utf8               ([Ljava/lang/String;)V
  #24 = Utf8               args
  #25 = Utf8               [Ljava/lang/String;
  #26 = Utf8               a1
  #27 = Utf8               Ljava/lang/String;
  #28 = Utf8               b1
  #29 = Utf8               a2
  #30 = Utf8               b2
  #31 = Utf8               StackMapTable
  #32 = Class              #25            // "[Ljava/lang/String;"
  #33 = Class              #36            // java/lang/String
  #34 = Class              #51            // java/io/PrintStream
  #35 = NameAndType        #16:#17        // "<init>":()V
  #36 = Utf8               java/lang/String
  #37 = Utf8               11
  #38 = NameAndType        #16:#52        // "<init>":(Ljava/lang/String;)V
  #39 = NameAndType        #53:#54        // intern:()Ljava/lang/String;
  #40 = Class              #55            // java/lang/System
  #41 = NameAndType        #56:#57        // out:Ljava/io/PrintStream;
  #42 = Class              #51            // java/io/PrintStream
  #43 = NameAndType        #58:#59        // println:(Z)V
  #44 = Utf8               java/lang/StringBuilder
  #45 = Utf8               a
  #46 = NameAndType        #60:#61        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #47 = NameAndType        #62:#54        // toString:()Ljava/lang/String;
  #48 = Utf8               aa
  #49 = Utf8               Test
  #50 = Utf8               java/lang/Object
  #51 = Utf8               java/io/PrintStream
  #52 = Utf8               (Ljava/lang/String;)V
  #53 = Utf8               intern
  #54 = Utf8               ()Ljava/lang/String;
  #55 = Utf8               java/lang/System
  #56 = Utf8               out
  #57 = Utf8               Ljava/io/PrintStream;
  #58 = Utf8               println
  #59 = Utf8               (Z)V
  #60 = Utf8               append
  #61 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #62 = Utf8               toString
{
  public Test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   LTest;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=5, args_size=1
         0: new           #2                  // class java/lang/String
         3: dup
         4: ldc           #3                  // String 11
         6: invokespecial #4                  // Method java/lang/String."<init>":(Ljava/lang/String;)V
         9: astore_1
        10: aload_1
        11: invokevirtual #5                  // Method java/lang/String.intern:()Ljava/lang/String;
        14: pop
        15: ldc           #3                  // String 11
        17: astore_2
        18: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
        21: aload_1
        22: aload_2
        23: if_acmpne     30
        26: iconst_1
        27: goto          31
        30: iconst_0
        31: invokevirtual #7                  // Method java/io/PrintStream.println:(Z)V
        34: new           #8                  // class java/lang/StringBuilder
        37: dup
        38: invokespecial #9                  // Method java/lang/StringBuilder."<init>":()V
        41: new           #2                  // class java/lang/String
        44: dup
        45: ldc           #10                 // String a
        47: invokespecial #4                  // Method java/lang/String."<init>":(Ljava/lang/String;)V
        50: invokevirtual #11                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        53: new           #2                  // class java/lang/String
        56: dup
        57: ldc           #10                 // String a
        59: invokespecial #4                  // Method java/lang/String."<init>":(Ljava/lang/String;)V
        62: invokevirtual #11                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        65: invokevirtual #12                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        68: astore_3
        69: aload_3
        70: invokevirtual #5                  // Method java/lang/String.intern:()Ljava/lang/String;
        73: pop
        74: ldc           #13                 // String aa
        76: astore        4
        78: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
        81: aload_3
        82: aload         4
        84: if_acmpne     91
        87: iconst_1
        88: goto          92
        91: iconst_0
        92: invokevirtual #7                  // Method java/io/PrintStream.println:(Z)V
        95: return
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      96     0  args   [Ljava/lang/String;
           10      86     1    a1   Ljava/lang/String;
           18      78     2    b1   Ljava/lang/String;
           69      27     3    a2   Ljava/lang/String;
           78      18     4    b2   Ljava/lang/String;
      StackMapTable: number_of_entries = 4
        frame_type = 255 /* full_frame */
          offset_delta = 30
          locals = [ class "[Ljava/lang/String;", class java/lang/String, class java/lang/String ]
          stack = [ class java/io/PrintStream ]
        frame_type = 255 /* full_frame */
          offset_delta = 0
          locals = [ class "[Ljava/lang/String;", class java/lang/String, class java/lang/String ]
          stack = [ class java/io/PrintStream, int ]
        frame_type = 255 /* full_frame */
          offset_delta = 59
          locals = [ class "[Ljava/lang/String;", class java/lang/String, class java/lang/String, class java/lang/String, class java/lang/String ]
          stack = [ class java/io/PrintStream ]
        frame_type = 255 /* full_frame */
          offset_delta = 0
          locals = [ class "[Ljava/lang/String;", class java/lang/String, class java/lang/String, class java/lang/String, class java/lang/String ]
          stack = [ class java/io/PrintStream, int ]
}

查看字节码可以看出,字符串变量使用加号连接,底层是使用StringBuilder.append实现的。

以下情况的字符串相加会使用StringBuilder.append实现

String a2 = new String("a") + new String("a");

以下情况的字符串相加在程序编译的时候就会将字符串拼接好了

String a = "11" + "22";
final String b = "33";
final String c = "44";
String d = b + c;
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!