We rarely use the javap tool because there are many good decompilation tools, but here I will introduce this tool not to use it for decompilation, but to view the bytecode generated by the java compiler for us, by comparing bytecode and source code, we can find many problems. A very important role is to understand the working mechanism inside the compiler.
Public class Main {
Public static void main (String [] args ){
String s = "abc ";
String ss = "OK" + s + "xyz" + 5;
System. out. println (ss );
}
}
Before decompilation, you must compile this class: javac-g Main. java (this option is required to use the-g parameter because the following javap-l output is required)
After compilation, we can use different options to see different effects:
1. first look at the simplest case without parameters: javap Main:
The public information of the class will be promised without parameters, including the Members and methods.
From the above output, we have determined two pieces of knowledge: if the class is not displayed and derived from other classes, it is derived from the Object; if there is no declarative constructor for the class display, then the compiler will generate a default constructor for it (the constructor without parameters)
2. javap-c Main
Similar to the output without parameters, the specific bytecode of the method is displayed later. From this output, we can learn more.
From the code above, it is easy to see that although "+" is used in the source program, "+" is still converted into StringBuilder during compilation. Therefore, we can draw a conclusion that the StringBuilder class is actually used in Java regardless of the method used for string connection.
3. javap-l Main
-L the parameter displays the row number and local variable table.
4. javap-p Main
The-p Parameter prints additional private member and method information, because this class does not output the same
These parameters can almost constitute the most commonly used collection of javap. The most commonly used parameter is the-c option, because the bytecode information can be printed, the detailed meanings of these bytecode are defined in the Java Virtual Machine specification. If you are interested, you can view the relevant information!
5. javap-s Main
Output internal type signature
6. javap-v Main
Output stack size, number of method parameters