how to disassemble java class file

本文通过解析Java字节码来比较不同增量运算符(i++, ++i, i+=1)的性能差异。通过使用javap工具进行反汇编,发现这些运算符在字节码层面并无区别,因此它们在理想条件下的性能表现应该是相同的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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.java
public 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 return

Method int preIncrement()
   0 iconst_0
   1 istore_1
   2 iinc 1 1
   5 iload_1
   6 ireturn

Method int postIncrement()
   0 iconst_0
   1 istore_1
   2 iinc 1 1
   5 iload_1
   6 ireturn

Method int negative()
   0 iconst_0
   1 istore_1
   2 iinc 1 1
   5 iload_1
   6 ireturn

Method int plusEquals()
   0 iconst_0
   1 istore_1
   2 iinc 1 1
   5 iload_1
   6 ireturn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值