Use of javap

Source: Internet
Author: User

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

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.