Easy to read bytecode files

  • invokeinterface : call interface methods

  • invokespecial method calls the initialize method, private method, or the parent class definition:

  • invokestatic : call the static method

  • invokevirtual : call an instance method

package com.xiaobu.test.StringDemo;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2019/4/30 16:04
 * @description V1.0 走StringBuilder
 */
public class StringDemo3 {
    public static void main(String[] args) {
    test1();
    }
    //结果为false
    private static void test1() {
        String str = "123";
        String a = "123";
        String str2 = a + "";
        System.out.println(str==str2);
    }

}

Use idea plug decompile bytecode files

Classfile /E:/Practise/SpringBoot/demo/target/classes/com/xiaobu/test/StringDemo/StringDemo3.class
  Last modified 2019-10-22; size 1020 bytes
  MD5 checksum fd43b6950ccbd87c2bdd41457f4e50b1
  Compiled from "StringDemo3.java"
public class com.xiaobu.test.StringDemo.StringDemo3
  minor version: 0
  major version: 52   //表明是JDK1.8
  flags: ACC_PUBLIC, ACC_SUPER  //类的访问标志
Constant pool:        //常量池
   #1 = Methodref          #12.#35        // java/lang/Object."<init>":()V   #+数字表示第几个常量 这个指向父类的构造方法
   #2 = Methodref          #11.#36        // com/xiaobu/test/StringDemo/StringDemo3.test1:()V  指向test1()方法 返回值void
   #3 = String             #37            // 123
   #4 = Class              #38            // java/lang/StringBuilder
   #5 = Methodref          #4.#35         // java/lang/StringBuilder."<init>":()V
   #6 = Methodref          #4.#39         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   #7 = String             #40            //
   #8 = Methodref          #4.#41         // java/lang/StringBuilder.toString:()Ljava/lang/String;
   #9 = Fieldref           #42.#43        // java/lang/System.out:Ljava/io/PrintStream;
  #10 = Methodref          #44.#45        // java/io/PrintStream.println:(Z)V
  #11 = Class              #46            // com/xiaobu/test/StringDemo/StringDemo3
  #12 = Class              #47            // java/lang/Object
  #13 = Utf8               <init>
  #14 = Utf8               ()V
  #15 = Utf8               Code
  #16 = Utf8               LineNumberTable
  #17 = Utf8               LocalVariableTable
  #18 = Utf8               this
  #19 = Utf8               Lcom/xiaobu/test/StringDemo/StringDemo3;
  #20 = Utf8               main
  #21 = Utf8               ([Ljava/lang/String;)V
  #22 = Utf8               args
  #23 = Utf8               [Ljava/lang/String;
  #24 = Utf8               MethodParameters
  #25 = Utf8               test1
  #26 = Utf8               str
  #27 = Utf8               Ljava/lang/String;
  #28 = Utf8               a
  #29 = Utf8               str2
  #30 = Utf8               StackMapTable
  #31 = Class              #48            // java/lang/String
  #32 = Class              #49            // java/io/PrintStream
  #33 = Utf8               SourceFile
  #34 = Utf8               StringDemo3.java
  #35 = NameAndType        #13:#14        // "<init>":()V
  #36 = NameAndType        #25:#14        // test1:()V
  #37 = Utf8               123
  #38 = Utf8               java/lang/StringBuilder
  #39 = NameAndType        #50:#51        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #40 = Utf8
  #41 = NameAndType        #52:#53        // toString:()Ljava/lang/String;
  #42 = Class              #54            // java/lang/System
  #43 = NameAndType        #55:#56        // out:Ljava/io/PrintStream;
  #44 = Class              #49            // java/io/PrintStream
  #45 = NameAndType        #57:#58        // println:(Z)V
  #46 = Utf8               com/xiaobu/test/StringDemo/StringDemo3
  #47 = Utf8               java/lang/Object
  #48 = Utf8               java/lang/String
  #49 = Utf8               java/io/PrintStream
  #50 = Utf8               append
  #51 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #52 = Utf8               toString
  #53 = Utf8               ()Ljava/lang/String;
  #54 = Utf8               java/lang/System
  #55 = Utf8               out
  #56 = Utf8               Ljava/io/PrintStream;
  #57 = Utf8               println
  #58 = Utf8               (Z)V
{
  public com.xiaobu.test.StringDemo.StringDemo3(); //默认构造方法
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:                                    //code表示代码 stack:最大操作数栈 locals:局部变量锁存储的空间:4个字节大小
      stack=1, locals=1, args_size=1        // args_size方法参数个数
         0: aload_0                         //将第一个引用类型本地变量推送至栈顶
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return                            //方法返回
      LineNumberTable:
        line 9: 0                             //第九行源码对应 字节码行号0: aload_0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/xiaobu/test/StringDemo/StringDemo3;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=0, locals=1, args_size=1
         0: invokestatic  #2                  // Method test1:()V
         3: return                            //方法返回
      LineNumberTable:
        line 11: 0                            //第十一行源码对应 字节码行号0: invokestatic  #2
        line 12: 3                            //第十二行源码对应 字节码行号3: return
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       4     0  args   [Ljava/lang/String;
    MethodParameters:
      Name                           Flags
      args
}
SourceFile: "StringDemo3.java"

As can be seen above, the code underlying StringBuilder away, splicing string, and then calls the StringBuilder toString method.

package com.xiaobu.test.StringDemo;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2019/4/30 16:04
 * @description V1.0 走String
 */
public class StringDemo4 {
    public static void main(String[] args) {
    test1();
    }


    //结果为true
    private static void test1() {
        String str = "11";
        String a = "11";
        String str2 = "11" + "";
        System.out.println(str==str2);
    }





}

Decompile bytecode files

Classfile /E:/Practise/SpringBoot/demo/target/classes/com/xiaobu/test/StringDemo/StringDemo4.class
  Last modified 2019-10-22; size 851 bytes
  MD5 checksum 045bcada94c8d6c922754d92f6242259
  Compiled from "StringDemo4.java"
public class com.xiaobu.test.StringDemo.StringDemo4
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #7.#30         // java/lang/Object."<init>":()V
   #2 = Methodref          #6.#31         // com/xiaobu/test/StringDemo/StringDemo4.test1:()V
   #3 = String             #32            // 11
   #4 = Fieldref           #33.#34        // java/lang/System.out:Ljava/io/PrintStream;
   #5 = Methodref          #35.#36        // java/io/PrintStream.println:(Z)V
   #6 = Class              #37            // com/xiaobu/test/StringDemo/StringDemo4
   #7 = Class              #38            // java/lang/Object
   #8 = Utf8               <init>
   #9 = Utf8               ()V
  #10 = Utf8               Code
  #11 = Utf8               LineNumberTable
  #12 = Utf8               LocalVariableTable
  #13 = Utf8               this
  #14 = Utf8               Lcom/xiaobu/test/StringDemo/StringDemo4;
  #15 = Utf8               main
  #16 = Utf8               ([Ljava/lang/String;)V
  #17 = Utf8               args
  #18 = Utf8               [Ljava/lang/String;
  #19 = Utf8               MethodParameters
  #20 = Utf8               test1
  #21 = Utf8               str
  #22 = Utf8               Ljava/lang/String;
  #23 = Utf8               a
  #24 = Utf8               str2
  #25 = Utf8               StackMapTable
  #26 = Class              #39            // java/lang/String
  #27 = Class              #40            // java/io/PrintStream
  #28 = Utf8               SourceFile
  #29 = Utf8               StringDemo4.java
  #30 = NameAndType        #8:#9          // "<init>":()V
  #31 = NameAndType        #20:#9         // test1:()V
  #32 = Utf8               11
  #33 = Class              #41            // java/lang/System
  #34 = NameAndType        #42:#43        // out:Ljava/io/PrintStream;
  #35 = Class              #40            // java/io/PrintStream
  #36 = NameAndType        #44:#45        // println:(Z)V
  #37 = Utf8               com/xiaobu/test/StringDemo/StringDemo4
  #38 = Utf8               java/lang/Object
  #39 = Utf8               java/lang/String
  #40 = Utf8               java/io/PrintStream
  #41 = Utf8               java/lang/System
  #42 = Utf8               out
  #43 = Utf8               Ljava/io/PrintStream;
  #44 = Utf8               println
  #45 = Utf8               (Z)V
{
  public com.xiaobu.test.StringDemo.StringDemo4();
    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
      LineNumberTable:
        line 9: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/xiaobu/test/StringDemo/StringDemo4;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=0, locals=1, args_size=1
         0: invokestatic  #2                  // Method test1:()V
         3: return
      LineNumberTable:
        line 11: 0
        line 12: 3
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       4     0  args   [Ljava/lang/String;
    MethodParameters:
      Name                           Flags
      args
}
SourceFile: "StringDemo4.java"

Go directly above the strings together.

Reference:
Easy to read bytecode
-depth understanding of JVM

Published 152 original articles · won praise 18 · views 70000 +

Guess you like

Origin blog.csdn.net/tanhongwei1994/article/details/102682977