Using JAVAP to help you understand Java features

Source: Internet
Author: User
Tags count goto reference return string tostring stringbuffer
Compiling Java developers is 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 dump information for a class and its methods to the 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.
  
JAVAP is quite useful when you need to look at what the compiler has done for you or for you, or if you want to see how the changes in the code affect the compiled class file.
  
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 use of this class? The CC option runs JAVAP output. The-C option tells the JAVAP to disassemble the byte code encountered in the class.
  
The operating 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
  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 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, where Withstringbuffer invokes only the existing StringBuffer append method, does not create a new object, and does not have a new String reference at each loop.
  
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 the 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 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.

Related Article

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.