Use javap disassembly To Help You Understand Java features (use javap to view class files in depth)
Author: builder.com
Use javap to view class files in Depth
Java developers are familiar with using stringbuffer in a loop instead of concatenating string objects for optimal performance. However, most developers have never compared the differences between the two methods. In the Java Development Kit (JDK), a tool called javap can tell you why this can achieve the best performance.
Javap outputs some dump information of a class and its methods to the standard output. This tool does not decompile the code into Java source code, but it will decompile the byte code into byte code instructions defined by Java Virtual Machine specifications.
Javap is useful when you need to check the compiler for you or for you, or if you want to see the impact of a code change on the compiled class files.
Now we use the stringbuffer and string we mentioned above as an example. The following is a class specially designed for the example. It has two methods and returns a string composed of numbers 0 to n, where n is provided by the caller. The only difference between the two methods is that one uses string to build the result, and the other uses stringbuffer to build the result.
Public class javaptip {
Public static void main (string [] ARGs ){
}
Private Static string withstrings (INT count ){
String S = "";
For (INT I = 0; I <count; I ++ ){
S + = I;
}
Return S;
}
Private Static string withstringbuffer (INT count ){
Stringbuffer sb = new stringbuffer ();
For (INT I = 0; I <count; I ++ ){
SB. append (I );
}
Return sb. tostring ();
}
}
Now let's take a look at the output of running javap for this class using the-C option. -The C option tells javap to disassemble the byte code encountered in the class.
Run as follows:
> Javap-C javaptip
The output of this command is:
Method java. Lang. String withstrings (INT)
0 LDC #2
2 astore_1
3 iconst_0
4 istore_2
5 goto 30
8 new #3
11 DUP
12 invokespecial #4
15 aload_1
16 invokevirtual #5
19 iload_2
20 invokevirtual #6
23 invokevirtual #7
26 astore_1
27 iinc 2 1
30 iload_2
31 iload_0
32 if_icmplt 8
35 aload_1
36 areturn
Method java. Lang. String withstringbuffer (INT)
0 new #3
3 DUP
4 invokespecial #4
7 astore_1
8 iconst_0
9 istore_2
10 goto 22
13 aload_1
14 iload_2
15 invokevirtual #6
18 pop
19 iinc 2 1
22 iload_2
23 iload_0
24 if_icmplt 13
27 aload_1
28 invokevirtual #7
31 areturn
If you haven't read the Java assembler before, this output will be difficult for you, but you should be able to see that the withstring method creates a stringbuffer instance each time it loops. It then adds the current value of the existing string to the stringbuffer, and then appends the current value of the loop. Finally, it calls tostring for the buffer and assigns the result to the existing string reference.
The withstringbuffer method is the opposite of this method. In each loop, withstringbuffer only calls the append method of the existing stringbuffer. No new object is created, and no new string reference is provided.
In this case, we already know that using stringbuffer to replace string is a good practice, but what if we do not know? So javap can help us find the answer.HereYou can see more detailed explanations about string and stringbuffer.
You don't need a Java anti-assembler often, but when you need it, it is of course a good thing to know that your machine already has one and its usage is quite simple. If you are interested, read a book and check other options of javap-you may find the features required in your environment.
--------------------------------------------------------------------------------
Author: David petersheim is an application development director of genscape. He designs and develops server-side applications to obtain and process real-time energy data.