View RSS Feed

My Java Tips

Javap

Rate this Entry
by , 05-24-2011 at 01:58 PM (1637 Views)
Javap is shiped with JDK and it really very useful when you want to see what your compiler is doing to your code. It generates source code from a compiled class file.

Javap is a utility provided in Java Development Kit (JDK) that can be used to improve source code. this is done by generating source code from compiled code and seeing how our code was treated by the Compiler. Later code improvements can be made.

A very interesting example can be to see how source code having StringBuffer in a loop for concatenating String objects was treated by the Compiler.

Javap takes a class and dumps information about its methods to standard out. It doesn't decompile the code into Java source code, but it will disassemble the bytecode into the bytecode instructions defined by the Java Virtual Machine specification.

Time for an example. StringBuffer vs. String scenario is a well known thing to developers. You know that using String for concatenation is an expensive operation since compiler uses StringBuffer internally. Lets validate this fact.

Java Code:
public class Example{
    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();
    }
}
Presented class has two methods.

  • withStrings(...): concatenates to String
  • withStringBuffer(...): concatenates to StringBuffer


Both methods take n as argument and concatenation operation is performed n times.

Compile the class and you will get class file. Lets see what javap outputs when it's run against the class with the -c option.

The -c switch tells javap to disassemble the bytecode found in the class.

Java Code:
<pre lang="java">
>javap -c Example
You will see following output on console:

Java Code:
Method java.lang.String withStrings(int)
   0 ldc #2 <String "">
   2 astore_1
   3 iconst_0
   4 istore_2
   5 goto 30
   8 new #3 <Class java.lang.StringBuffer>
  11 dup
  12 invokespecial #4 <Method java.lang.StringBuffer()>
  15 aload_1
  16 invokevirtual #5 <Method java.lang.StringBuffer append(java.lang.String)>
  19 iload_2
  20 invokevirtual #6 <Method java.lang.StringBuffer append(int)>
  23 invokevirtual #7 <Method java.lang.String toString()>
  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 <Class java.lang.StringBuffer>
   3 dup
   4 invokespecial #4 <Method java.lang.StringBuffer()>
   7 astore_1
   8 iconst_0
   9 istore_2
  10 goto 22
  13 aload_1
  14 iload_2
  15 invokevirtual #6 <Method java.lang.StringBuffer append(int)>
  18 pop
  19 iinc 2 1
  22 iload_2
  23 iload_0
  24 if_icmplt 13
  27 aload_1
  28 invokevirtual #7 <Method java.lang.String toString()>
  31 areturn
The output sure is a bit cryptic but things are obvious.

withString Method does the following:

  • Creates a new StringBuffer instance each time through the loop,
  • Appends the current value of the existing String to the StringBuffer,
  • Appends the current value of the loop,
  • Calls toString on the buffer and assigns the results to the existing String reference.


withStringBuffer Method does the following:

  • Calls the existing StringBuffer's append method each time through the loop,
  • No object creation and no new String references.


So using Java disassembler, we now know that using StringBuffer instead of String was a good idea.

Submit "Javap" to Facebook Submit "Javap" to Digg Submit "Javap" to del.icio.us Submit "Javap" to StumbleUpon Submit "Javap" to Google

Comments