Use JAVAP disassembly to help you understand Java features

Source: Internet
Author: User
Tags count goto reference return tostring stringbuffer
Use JAVAP disassembly to help you understand Java features (use JAVAP to drill down on class files)





Author: builder.com


use JAVAP to drill down into class files





Java developers are familiar with using StringBuffer in a loop instead of concatenating String objects for optimal performance. However, most developers never compare the differences between the byte codes produced by the two methods. In the Java Development Kit (JDK), there is a tool called JAVAP that can tell you why doing this can achieve optimal performance.











JAVAP outputs Some of the dump information for a class and its methods to standard output. The tool does not decompile code as Java source code, but it will disassemble the byte code into a byte code directive defined by the Java Virtual Machine specification.





is useful when you need to look at what the compiler has done for you or for you, or if you want to see how a code change affects the compiled class file. Javap





now take the StringBuffer and String we mentioned earlier as an example. The following is a class designed specifically for example, which has two methods that return a String of 0 to n digits, where n is provided by the caller. The only difference between the two methods is that one uses String to build the results, and the other uses StringBuffer to build the results.





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 look at the output of running JAVAP for this class using the –C option. The-C option tells the JAVAP to disassemble the byte code encountered in the class.





operation mode is 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


DUP


invokespecial #4


aload_1


invokevirtual #5


iload_2


invokevirtual #6


Invokevirtual #7


Astore_1


iinc 2 1


iload_2


Iload_0


IF_ICMPLT 8


aload_1


Areturn





method java.lang.String withstringbuffer (int)


0 New #3


3 DUP


4 invokespecial #4


7 Astore_1


8 Iconst_0


9 istore_2


Goto 22


aload_1


iload_2


invokevirtual #6


Pop


iinc 2 1


iload_2


Iload_0


IF_ICMPLT 13


aload_1


invokevirtual #7


Areturn





If you haven't seen a Java assembler before, this output will be more difficult for you to understand, but you should see that the Withstring method creates a new StringBuffer instance each time it loops. It then appends the current value of the existing String to the StringBuffer, and then appends the current value of the loop. Finally, it invokes toString on buffer and assigns the result to an existing String reference.




The
Withstringbuffer method is just the opposite of this method, and Withstringbuffer only invokes the Append method of the existing stringbuffer at each loop, without creating a new object or a new String reference.





in this case, we already know that using stringbuffer instead of String is a good practice, but what if we don't know? Then JAVAP can help us find the answer. Here you can see a more detailed explanation of String,stringbuffer





you don't often need a Java disassembler, but it's a good thing to know that your own machine already has one and use a fairly simple disassembler when you need it. If you're interested, read the other options for JAVAP-perhaps you'll find the features you need in your environment.











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.