Javap Introduction [favorites]

Source: Internet
Author: User
We may 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, I have used this tool in my previous articles. The original articles on this site are "in-depth analysis of Java class constructor" and "using string or stringbuffer".
The following is a specific example to illustrate the role of this tool. You do not need to use it in depth. This simple introduction and simple use will benefit you a lot.
Source code:
Class stringtest
{
Public static void main (string [] ARGs)
{
String result = "";
Result + = "OK ";
}
}
Before decompiling, you must compile this class: javac-G stringtest. 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 stringtest:
Compiled from stringtest. Java
Class stringtest extends java. Lang. object {
Stringtest ();
Public static void main (Java. Lang. String []);
}
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 stringtest:
Compiled from stringtest. Java
Class stringtest extends java. Lang. object {
Stringtest ();
Public static void main (Java. Lang. String []);
}

Method stringtest ()
0 aload_0
1 invokespecial #1 <method java. Lang. Object ()>
4 return

Method void main (Java. Lang. String [])
0 LDC #2 <string "">
2 astore_1
3 New #3 <class java. Lang. stringbuffer>
6 DUP
7 invokespecial #4 <method java. Lang. stringbuffer ()>
10 aload_1
11 invokevirtual #5 <method java. Lang. stringbuffer append (Java. Lang. String)>
14 LDC #6 <string "OK">
16 invokevirtual #5 <method java. Lang. stringbuffer append (Java. Lang. String)>
19 invokevirtual #7 <method java. Lang. String tostring ()>
22 astore_1
23 return
The extra bytecode is printed with the-p parameter.
Similar to the output without parameters, the specific bytecode of the method is displayed later. From this output, we can learn more, first, the content of the default constructor generated by the compiler is to call the constructor super () of the parent class (it should be noted that the default constructor is not called in the source code of DJ decompilation, this may be an optimization of the anti-compiler. For details about the bytecode information of the main () method, refer to the description in "using string or stringbuffer.
3. javap-l stringtest:
Compiled from stringtest. Java
Class stringtest extends java. Lang. object {
Stringtest ();
Public static void main (Java. Lang. String []);
}

Line numbers for method stringtest ()
Line 1: 0

Local variables for method stringtest ()
Stringtest this PC = 0, length = 5, slot = 0

Line numbers for method void main (Java. Lang. String [])
Line 5: 0
Line 6: 3
Line 7: 23

Local variables for method void main (Java. Lang. String [])
Java. Lang. String [] ARGs Pc = 0, length = 24, slot = 0
Java. Lang. String result Pc = 3, length = 20, slot = 1
-L the parameter displays the row number and local variable table.
From the above output, we can obtain the bytecode information of the variables and method source code in the method, for example, corresponding to the main () method. Its variables are the input parameter ARGs and the local variable result, the first line of the source code of the method corresponds to the 5th offset of the bytecode, and the second line corresponds to the 0th offset of the bytecode, the 7th line corresponds to the 23rd offset of the bytecode (see the offset before the javap-C output). The 7th line actually has no statement, but there is an implicit return, the offset 23 actually corresponds to the return call.
4. javap-P stringtest:
Compiled from stringtest. Java
Class stringtest extends java. Lang. object {
Stringtest ();
Public static void main (Java. Lang. String []);
}
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. I may write some articles on this information in the future.

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.