Use Javap decompilation To Help You Understand Java features

Source: Internet
Author: User

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 how to use this class? The Cc option is used to run the javap output. -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. Here you 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.

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.