how to disassemble java class file

来源:互联网 发布:软件销售开票税率 编辑:程序博客网 时间:2024/05/22 08:15

 when we use java command to compile a java file to bytecode file ,just have suffix .class. And in software development, we probally use other's class file to implement our function. Class file is a type of file call bytecode, just like the assemble file, and can run on java virtual machine. If we want to know what is on earth in a class file, we can use command javap  ** provided by Sun. and if you want to see the bytecode of .class file, you can use javap -c filename, you can see the bytecode of this file. Following is an example.

Disassembling Java Classes

A few months ago, on our local Java User Group discussion forum, I casually asked the question what was faster: i++, ++i or i+=1. I also mentioned that the answer might surprise them. Wanting to encourage thinking, I never gave the answer ;-)

Yesterday, one of my readers, a bright young student at the Cape Town Technical University, sent me an answer based on a thorough investigation through microbenchmarks. His conclusion was correct, but the approach to getting to the conclusion led to a lot of effort being spent on his part.

The easiest way to decide which is fastest is actually to disassemble the Java Class. Note that I am saying disassemble, not decompile. Let's look at the following class:

public class Increment {  public int preIncrement() {    int i = 0;    ++i;     return i;  }  public int postIncrement() {    int i = 0;    i++;     return i;  }  public int negative() {    int i = 0;    i-=-1;     return i;  }  public int plusEquals() {    int i = 0;    i += 1;     return i;  }}

I may ask you: which method is the fastest, which is the slowest? C programmers would probably say that the fastest are i++ and ++i. When you try running these methods many times, you will notice slight differences between them, based on which you run first, how soon the hotspot kicks in, whether you use client or server hotspot, whether you are busy playing an MP3 on your machine while running the test and the phase of the moon.

Instead of measuring the performance, why not investigate what the Java compiler did? You can disassemble a class with the standard javap tool available in your JAVA_HOME/bin directory:

javap -c Increment

Note that the class must already be compiled. The result is the following:

Compiled from Increment.javapublic class Increment extends java.lang.Object {    public Increment();    public int preIncrement();    public int postIncrement();    public int negative();    public int plusEquals();}Method Increment()   0 aload_0   1 invokespecial #9 <Method java.lang.Object()>   4 returnMethod int preIncrement()   0 iconst_0   1 istore_1   2 iinc 1 1   5 iload_1   6 ireturnMethod int postIncrement()   0 iconst_0   1 istore_1   2 iinc 1 1   5 iload_1   6 ireturnMethod int negative()   0 iconst_0   1 istore_1   2 iinc 1 1   5 iload_1   6 ireturnMethod int plusEquals()   0 iconst_0   1 istore_1   2 iinc 1 1   5 iload_1   6 ireturn
原创粉丝点击