当前位置:网站首页>How to view bytecode under idea?

How to view bytecode under idea?

2020-12-07 13:01:13 User 1516716

This article teaches you IDEA Three ways to view bytecode in as well as jdk String splicing 、 The operation process of automatic packing and unpacking .

The first thing to know is jdk、jre、jvm The relationship between the three :

JDK:Java Development kit , Compilation is also included java Source compiler javac, For development . JRE:Java Runtime environment , Some basic class libraries , For operation . JVM:Java virtual machine . It only knows xxx.class This type of document , It will be able to class The bytecode instructions in the file are identified and used for different operating systems API Complete the action .

Java The general execution process of the program is as follows :

java Program execution flow

So ,jvm yes Java The core of being able to cross platform .

Create a new one Test.java file , And compile . Let's get to the point , Introduce three ways to view Bytecode .

1、 Use JDK Bring their own Of javap

javap yes jdk A disassembly tool that comes with it , Can be used to view compiled bytecode .

After compiling , Locate your .class file

But look at the bytecode , It's very troublesome. , You need to compile . Find this every time .class file , Then input javap -c xxx.class

What's the convenient way to do it ?

Of course there are .

open Settings ——》External Tools

name: The name of the plug-in , I have written MyjavapProgram: The plugin path , I am here E:\JDK1.8\bin\javap.exe, Write according to your local path .Arguments: Parameters , direct writing -c FileNameWithoutExtension.classWorking directory:class Output directory , direct writing OutputPath\FileDirRelativeToSourcepath

Click on OK , Then select Yours .java Source file , Click on Tools——》External Tools ——》Myjavap

The above can be very convenient to see the console output bytecode .

2、IDEA Bring their own show byteCode

Click on your .java Source file , then Click on the menu bar View ——》Show byteCode

Then a bytecode window will pop up .

3、jclasslib plug-in unit

Search the plug-in market jclasslib, Click on the install .

restart IDEA.

menu bar View——》Show Bytecode With jclasslib

Bytecode means

Here's what bytecode means .

For more instructions on bytecode, refer to :https://blog.csdn.net/qq_31407255/article/details/88978630

I'm a local Test.java :

public class Test {
    public static void main(String[] args) {
        String a = "I am ";
        String b = "HaC";
        String c = a + b;
        String d = "I am " + "HaC";
        System.out.println(c == d); //false
        Integer i =100; // Packing 
        int j = i;  // Unpacking 
        System.out.println(i == j); //true
    }
}

This is the use of jclasslib Bytecode :

 0 ldc #2 <I am >       # JVM use ldc Instruction pushes constants onto the stack 
 2 astore_1            #  Save top of stack reference type value to local variable 1 in , namely a
 3 ldc #3 <HaC>
 5 astore_2
 6 new #4 <java/lang/StringBuilder>        #  Create a new object instance , namely c
 9 dup                                  #  Copy a word of data at the top of the stack , Press the copied data onto the stack .
10 invokespecial #5 <java/lang/StringBuilder.<init>> #  Compile time method binding calls methods , Compile time call StringBuilder, amount to  new StringBuilder("I am "), The same below 
13 aload_1                                         #  From local variable 1, to new StringBuilder("I am ") References to , Load reference type values onto the stack .
14 invokevirtual #6 <java/lang/StringBuilder.append> #  This call StringBuilder Of appen Method , Splicing at runtime 
17 aload_2
18 invokevirtual #6 <java/lang/StringBuilder.append>
21 invokevirtual #7 <java/lang/StringBuilder.toString>    #  Last call  toString Method , namely  "I am HaC".toString
24 astore_3                                         
25 ldc #8 <I am HaC>      #  here jvm Directly optimized , hold   d  Turned into "I am HaC"
27 astore 4
29 getstatic #9 <java/lang/System.out>
32 aload_3
33 aload 4
35 if_acmpne 42 (+7)
38 iconst_1
39 goto 43 (+4)
42 iconst_0
43 invokevirtual #10 <java/io/PrintStream.println>
46 bipush 100
48 invokestatic #11 <java/lang/Integer.valueOf>        # Integer i =100; Packing , Equal to Integer i =Integer.valueOf(100);
51 astore 5
53 aload 5
55 invokevirtual #12 <java/lang/Integer.intValue>   # int j = i;  Unpacking , Equal to  int j = i.intValue();
58 istore 6
60 getstatic #9 <java/lang/System.out>
63 aload 5
65 invokevirtual #12 <java/lang/Integer.intValue>  # Integer and int Compare , Integer Automatically call intValue Method , That is, unpacking 
68 iload 6
70 if_icmpne 77 (+7)
73 iconst_1
74 goto 78 (+4)
77 iconst_0
78 invokevirtual #10 <java/io/PrintStream.println>
81 return

So ,

String c = a + b;
String d = "I am " + "HaC";
System.out.println(c == d); //false

c Is the use of the StringBuilder Splicing , Finally, the memory reference to the heap is returned , and d yes jvm After optimization , Pointing directly to the constant pool "I am HaC", The two are not equal .

And when Integer object and int Basic types When comparing ,Integer It will unpack automatically , Turn it into int type , Compare values , The two are naturally equal .

 Integer i =100; // Packing 
 int j = i;  // Unpacking 
 System.out.println(i == j); //true

This article is from WeChat official account. - IT pasture (itmuch_com)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2020-12-03

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[User 1516716]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012071258304053.html