前记:作为一名JAVA Developer,每次打开Eclipse,查找一个没有源码的类时,都会看到一个这样的画面:

大意是:这个jar文件,没有附带源码。紧接着后面的就看不懂了,很好奇下面的一部分是什么东东。

原来是大名鼎鼎的java bytecode,那么如何读懂这些天书一样的代码呢?在IBM的网站上有一篇好文章,特地抄了下来。

http://www.ibm.com/developerworks/library/it-haggar_bytecode/index.html

Java bytecode:

Understanding bytecode makes you a better programmer

This article gives you an understanding of Java bytecode that will enable you to be a better programmer. Like a C or C++ compiler translates source code into assembler code, Java compilers translate Java source code into bytecode. Java programmers should take the time to understand what the bytecode is, how it works, and most importantly, what bytecode is being generated by the Java compiler. In some cases, the bytecode generated is not what you expect.

9 Comments

Share:

Peter Haggar (haggar@us.ibm.com), Senior Software Engineer, IBM

01 July 2001

  • Table of contents

The information about bytecode, as well as the bytecode presented here, is based on the Java 2 SDK Standard Edition v1.2.1 javac compiler. The bytecode generated by other compilers may vary slightly.

Why understand bytecode?

Bytecode is the intermediate representation of Java programs just as assembler is the intermediate representation of C or C++ programs. The most knowlegable C and C++ programmers know the assembler instruction set of the processor for which they are compiling. This knowledge is crucial when debugging and doing performance and memory usage tuning. Knowing the assembler instructions that are generated by the compiler for the source code you write, helps you know how you might code differently to achieve memory or performance goals. In addition, when tracking down a problem, it is often useful to use a debugger to disassemble the source code and step through the assembler code that is executing.

An often overlooked aspect of Java is the bytecode that is generated by the javac compiler. Understanding bytecode and what bytecode is likely to be generated by a Java compiler helps the Java programmer in the same way that knowledge of assembler helps the C or C++ programmer.

The bytecode is your program. Regardless of a JIT or Hotspot runtime, the bytecode is an important part of the size and execution speed of your code. Consider that the more bytecode you have, the bigger the .class file is and the more code that has to be compiled by a JIT or Hotspot runtime. The remainder of this article gives you an in depth look at Java bytecode.

Back to top

Generating bytecode

javac Employee.java
javap -c Employee > Employee.bc
Compiled from Employee.java
class Employee extends java.lang.Object {
public Employee(java.lang.String,int);
public java.lang.String employeeName();
public int employeeNumber();
} Method Employee(java.lang.String,int)
0 aload_0
1 invokespecial #3 <Method java.lang.Object()>
4 aload_0
5 aload_1
6 putfield #5 <Field java.lang.String name>
9 aload_0
10 iload_2
11 putfield #4 <Field int idNumber>
14 aload_0
15 aload_1
16 iload_2
17 invokespecial #6 <Method void storeData(java.lang.String, int)>
20 return Method java.lang.String employeeName()
0 aload_0
1 getfield #5 <Field java.lang.String name>
4 areturn Method int employeeNumber()
0 aload_0
1 getfield #4 <Field int idNumber>
4 ireturn Method void storeData(java.lang.String, int)
0 return

This class is very simple. It contains two instance variables, a constructor and three methods. The first five lines of the bytecode file list the file name that is used to generate this code, the class definition, its inheritance (by default, all classes inherit from java.lang.Object ), and its constructors and methods. Next, the bytecode for each of the constructors is listed. Then, each method is listed in alphabetical order with its associated bytecode.

You might notice on closer inspection of the bytecode that certain opcodes are prefixed with an `a' or an `i'. For example, in the Employee class constructor you see aload_0 and iload_2. The prefix is representative of the type that the opcode is working with. The prefix `a' means that the opcode is manipulating an object reference. The prefix `i' means the opcode is manipulating an integer. Other opcodes use `b' for byte, `c' for char, `d' for double, etc. This prefix gives you immediate knowledge about what type of data is being manipulated.

Note: Individual codes are generally referred to as opcode. Multiple opcode instructions are generally referred to as bytecode.

Back to top

The details

To understand the details of the bytecode, we need to discuss how a Java Virtual Machine (JVM) works regarding the execution of the bytecode. A JVM is a stack-based machine. Each thread has a JVM stack which stores frames. A frame is created each time a method is invoked, and consists of an operand stack, an array of local variables, and a reference to the runtime constant pool of the class of the current method. Conceptually, it might look like this:

Figure 1. A frame

The array of local variables, also called the local variable table, contains the parameters of the method and is also used to hold the values of the local variables. The parameters are stored first, beginning at index 0. If the frame is for a constructor or an instance method, the reference is stored at location 0. Then location 1 contains the first formal parameter, location 2 the second, and so on. For a static method, the first formal method parameter is stored in location 0, the second in location 1, and so on.

The size of the array of local variables is determined at compile time and is dependent on the number and size of local variables and formal method parameters. The operand stack is a LIFO stack used to push and pop values. Its size is also determined at compile time. Certain opcode instructions push values onto the operand stack; others take operands from the stack, manipulate them, and push the result. The operand stack is also used to receive return values from methods.

public String employeeName()
{
return name;
} Method java.lang.String employeeName()
0 aload_0
1 getfield #5 <Field java.lang.String name>
4 areturn

The bytecode for this method consists of three opcode instructions. The first opcode, aload_0, pushes the value from index 0 of the local variable table onto the operand stack. Earlier, it was mentioned that the local variable table is used to pass parameters to methods. The this reference is always stored at location 0 of the local variable table for constructors and instance methods. The this reference must be pushed because the method is accessing the instance data, name, of the class.

The next opcode instruction, getfield, is used to fetch a field from an object. When this opcode is executed, the top value from the stack, this, is popped. Then the #5 is used to build an index into the runtime constant pool of the class where the reference to name is stored. When this reference is fetched, it is pushed onto the operand stack.

The last instruction, areturn, returns a reference from a method. More specifically, the execution of areturn causes the top value on the operand stack, the reference to name, to be popped and pushed onto the operand stack of the calling method.

The employeeName method is fairly simple. Before looking at a more complex example, we need to examine the values to the left of each opcode. In the employeeName method's bytecode, these values are 0, 1, and 4. Each method has a corresponding bytecode array. These values correspond to the index into the array where each opcode and its arguments are stored. You might wonder why the values are not sequential. Since bytecode got its name because each instruction occupies one byte, why are the indexes not 0, 1, and 2? The reason is some of the opcodes have parameters that take up space in the bytecode array. For example, the aload_0 instruction has no parameters and naturally occupies one byte in the bytecode array. Therefore, the next opcode, getfield, is in location 1. However, areturn is in location 4. This is because the getfield opcode and its parameters occupy location 1, 2, and 3. Location 1 is used for the getfield opcode, location 2 and 3 are used to hold its parameters. These parameters are used to construct an index into the runtime constant pool for the class to where the value is stored. The following diagram shows what the bytecode array looks like for the employeeName method:

Figure 2. Bytecode array for employeeName method

Actually, the bytecode array contains bytes that represent the instructions. Looking at a .class file with a hex editor, you would see the following values in the bytecode array:

Figure 3. Values in the bytecode array

2A , B4 , and B0 correspond to aload_0, getfield, and areturn, respectively.

public Employee(String strName, int num)
{
name = strName;
idNumber = num;
storeData(strName, num);
} Method Employee(java.lang.String,int)
0 aload_0
1 invokespecial #3 <Method java.lang.Object()>
4 aload_0
5 aload_1
6 putfield #5 <Field java.lang.String name>
9 aload_0
10 iload_2
11 putfield #4 <Field int idNumber>
14 aload_0
15 aload_1
16 iload_2
17 invokespecial #6 <Method void storeData(java.lang.String, int)>
20 return

The first opcode instruction at location 0, aload_0, pushes the this reference onto the operand stack. (Remember, the first entry of the local variable table for instance methods and constructors is the this reference.)

The next opcode instruction at location 1, invokespecial, calls the constructor of this class's superclass. Because all classes that do not explicitly extend any other class implicitly inherit from java.lang.Object , the compiler provides the necessary bytecode to invoke this base class constructor. During this opcode, the top value from the operand stack, this, is popped.

The next two opcodes, at locations 4 and 5 push the first two entries from the local variable table onto the operand stack. The first value to be pushed is the this reference. The second value is the first formal parameter to the constructor, strName . These values are pushed in preparation for the putfield opcode instruction at location 6.

The putfield opcode pops the two top values off the stack and stores a reference to strName into the instance data name of the object referenced by this .

The next three opcode instructions at locations 9, 10, and 11 perform the same operation with the second formal parameter to the constructor, num , and the instance variable, idNumber .

The next three opcode instructions at locations 14, 15, and 16 prepare the stack for the storeData method call. These instructions push the this reference, strName , and num , respectively. The this reference must be pushed because an instance method is being called. If the method was declared static, the this reference would not need to be pushed. The strName and num values are pushed since they are the parameters to the storeData method. When the storeData method executes, the this reference, strName , and num , will occupy indexes 0, 1, and 2, respectively, of the local variable table contained in the frame for that method.

Back to top

Size and speed issues

Performance is a critical issue for many desktop and server systems that use Java. As Java moves from these systems to smaller embedded devices, size issues become important too. Knowing what bytecode is generated for a sequence of Java instructions can help you write smaller and more efficient code. For example, consider synchronization in Java. The following two methods return the top element from a stack of integers that is implemented as an array. Both methods use synchronization and are functionally equivalent:

public synchronized int top1()
{
return intArr[0];
}
public int top2()
{
synchronized (this) {
return intArr[0];
}
}

These methods, although using synchronization differently, are functionally identical. What is not obvious, however, is that they have different performance and size characteristics. In this case, top1 is approximately 13 percent faster than top2 as well as much smaller. Examine the generated bytecode to see how these methods differ. The comments are added to the bytecode to assist in understanding what each opcode does.

Method int top1()
0 aload_0 //Push the object reference(this) at index
//0 of the local variable table.
1 getfield #6 <Field int intArr[]>
//Pop the object reference(this) and push
//the object reference for intArr accessed
//from the constant pool.
4 iconst_0 //Push 0.
5 iaload //Pop the top two values and push the
//value at index 0 of intArr.
6 ireturn //Pop top value and push it on the operand
//stack of the invoking method. Exit. Method int top2()
0 aload_0 //Push the object reference(this) at index
//0 of the local variable table.
1 astore_2 //Pop the object reference(this) and store
//at index 2 of the local variable table.
2 aload_2 //Push the object reference(this).
3 monitorenter //Pop the object reference(this) and
//acquire the object's monitor.
4 aload_0 //Beginning of the synchronized block.
//Push the object reference(this) at index
//0 of the local variable table.
5 getfield #6 <Field int intArr[]>
//Pop the object reference(this) and push
//the object reference for intArr accessed
//from the constant pool.
8 iconst_0 //Push 0.
9 iaload //Pop the top two values and push the
//value at index 0 of intArr.
10 istore_1 //Pop the value and store it at index 1 of
//the local variable table.
11 jsr 19 //Push the address of the next opcode(14)
//and jump to location 19.
14 iload_1 //Push the value at index 1 of the local
//variable table.
15 ireturn //Pop top value and push it on the operand
//stack of the invoking method. Exit.
16 aload_2 //End of the synchronized block. Push the
//object reference(this) at index 2 of the
//local variable table.
17 monitorexit //Pop the object reference(this) and exit
//the monitor.
18 athrow //Pop the object reference(this) and throw
//an exception.
19 astore_3 //Pop the return address(14) and store it
//at index 3 of the local variable table.
20 aload_2 //Push the object reference(this) at
//index 2 of the local variable table.
21 monitorexit //Pop the object reference(this) and exit
//the monitor.
22 ret 3 //Return to the location indicated by
//index 3 of the local variable table(14).
Exception table: //If any exception occurs between
from to target type //location 4 (inclusive) and location
4 16 16 any //16 (exclusive) jump to location 16.

top2 is larger and slower than top1 because of how the synchronization and exception processing is done. Notice that top1 uses the synchronized method modifier, which does not generate extra code. By contrast, top2 uses a synchronized statement in the body of the method.

Using synchronized in the body of the method generates the bytecode for the monitorenter and monitorexit opcodes, as well as additional code to handle exceptions. If an exception is generated while executing inside of a synchronized block (a monitor), the lock is guaranteed to be released prior to exiting the synchronized block. The implementation of top1 is slightly more efficient than that of top2 ; this results in a very small performance gain.

When the synchronized method modifier is present, as in top1, the acquisition and subsequent release of the lock is not done with the monitorenter and monitorexit opcodes. Instead, when the JVM invokes a method, it checks for the ACC_SYNCHRONIZED property flag. If this flag is present, the thread that is executing acquires a lock, calls the method, and then releases the lock when the method returns. If an exception is thrown from a synchronized method, the lock is automatically released before the exception leaves the method.

Note: The ACC_SYNCHRONIZED property flag is included in a method's method_info structure if the synchronized method modifier is present.

Whether you use synchronized as a method modifier or with the synchronized block, there are size implications. Use synchronized methods only when your code requires synchronization and you understand the cost imposed by their usage. If an entire method needs to be synchronized, I prefer the method modifier over the synchronized block in order to produce smaller and slightly faster code.

This is just one example of using knowledge of bytecode to make your code smaller and faster; more information is found in my book, Practical Java.

Back to top

Compiler options

The javac compiler provides a few options that you need to know. The first is the -O option. The JDK documentation claims that -O will optimize your code for execution speed. Using -O with the javac compiler with the Sun Java 2 SDK has no effect on the generated code. Previous versions of the Sun javac compiler performed some rudimentary bytecode optimizations, but these have since been removed. The SDK documentation, however, has not been updated. The only reason -O remains as an option is for compatibility with older make files. Therefore, there is currently no reason to use it.

This also means that the bytecode that is generated by the javac compiler is not any better than the code that you write. For example, if you write a loop that contains an invariant, the invariant will not be removed from the loop by the javac compiler. Programmers are used to compilers from other languages that clean up badly written code. Unfortunately, javac does not do this. More importantly, the javac compiler does not perform simple optimizations like loop unrolling, algebraic simplification, strength reduction, and others. To get these benefits and other simple optimizations, the programmer must perform them on the Java source code and not rely on the javac compiler to perform them. There are many techniques you can use to make the Java compiler generate faster and smaller bytecode. Unfortunately, until Java compilers perform them, you must implement them yourself to achieve their benefits.

The javac compiler also supplies the -g and -g:none options. The -g option tells the compiler to generate all the debugging information. The -g:none option tells the compiler to generate no debugging information. Compiling with -g:none generates the smallest possible .class file. Therefore, this option should be used when trying to generate the smallest possible .class files prior to deployment.

Back to top

Java debuggers

One very useful feature that I have yet to see in a Java debugger is a disassembly view similar to that of a C or C++ debugger. Disassembling Java code would reveal the bytecode, much like disassembling C or C++ code reveals assembler code. In addition to this feature, another useful feature could be the ability to single step through the bytecode, executing one opcode at a time.

This level of functionality would allow the programmer to see, first hand, the bytecode generated by the Java compiler as well as step through it during debugging. The more information a programmer has about the code generated and executed, the better chance there is of avoiding problems. This type of debugger feature would also encourage programmers to look at, and understand, the bytecode that is executing for their source code.

Back to top

Summary

This article gives you an overview and general understanding of Java bytecode. The best programmers of any language understand the intermediate form that the high-level language is translated to before execution. With Java, this intermediate representation is bytecode. Understanding it, knowing how it works, and more importantly, knowing what bytecode is generated by the Java compiler for particular source code, is critical to writing the fastest and smallest code possible.

java字节码理解-入门的更多相关文章

  1. 【Java虚拟机1】Java字节码文件格式入门

    第一次学习看字节码文件,这个对工作没什么用,但是会提升内功. 首先介绍两个IDEA插件以及使用: BinEd:以16进制格式查看class文件 使用方法:右键class文件,点击Open as bin ...

  2. 学以致用,通过字节码理解:Java的内部类与外部类之私有域访问

    目录: 内部类的定义及用处 打开字节码理解内部类 一.内部类的定义及用处 内部类(inner class)是定义在另一个类中的类.使用内部类,我们可以: 访问该类定义所在的作用域中的数据,包括私有的数 ...

  3. 从1+1=2来理解Java字节码从1+1=2来理解Java字节码

    编译"1+1"代码 首先我们需要写个简单的小程序,1+1的程序,学习就要从最简单的1+1开始,代码如下: 写好java类文件后,首先执行命令javac TestJava.java ...

  4. 硬核万字长文,深入理解 Java 字节码指令(建议收藏)

    Java 字节码指令是 JVM 体系中非常难啃的一块硬骨头,我估计有些读者会有这样的疑惑,"Java 字节码难学吗?我能不能学会啊?" 讲良心话,不是我谦虚,一开始学 Java 字 ...

  5. 从Java源码到Java字节码

    Java最主流的源码编译器,javac,基本上不对代码做优化,只会做少量由Java语言规范要求或推荐的优化:也不做任何混淆,包括名字混淆或控制流混淆这些都不做.这使得javac生成的代码能很好的维持与 ...

  6. 在Eclipse里查看Java字节码

    要理解 Java 字节码,比较推荐的方法是自己尝试编写源码对照字节码学习.其中阅读 Java 字节码的工具必不可少.虽然javap可以以可读的形式展示出.class 文件中字节码,但每次改动源码都需调 ...

  7. 【转】在Eclipse里查看Java字节码

    要理解 Java 字节码,比较推荐的方法是自己尝试编写源码对照字节码学习.其中阅读 Java 字节码的工具必不可少.虽然javap可以以可读的形式展示出.class 文件中字节码,但每次改动源码都需调 ...

  8. Java字节码(.class文件)格式详解(一)

    原文链接:http://www.blogjava.net/DLevin/archive/2011/09/05/358033.html 小介:去年在读<深入解析JVM>的时候写的,记得当时还 ...

  9. 掌握Java字节码(转)

    Java是一门设计为运行于虚拟机之上的编程语言,因此它需要一次编译,处处运行(当然也是一次编写,处处测试).因此,安装到你系统上的JVM是原生的程序,而运行在它之上的代码是平台无关的.Java字节码就 ...

随机推荐

  1. 【BZOJ】2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2101 这个dp真是神思想orz 设状态f[i, j]表示i-j先手所拿最大值,注意,是先手 所以转移 ...

  2. html input size maxlength

    最近做项目用到input的size和maxlength属性,以前只顾用没有用心去看看这2个标签的区别,今天周末baidu了一下,有所理解.特记录于此!   <p>Name: <inp ...

  3. 将MathType公式粘贴到文档中的步骤

    MathType这款数学公式编辑器,常常是辅助于各种文档使用的.由于其编辑的公式与数学符号等都符号国际期刊的出版标准,并且上手容易,根本就不需要一个学习的过程,只要安装打开后就能直接上手使用.所以非常 ...

  4. 用原生JS模仿jquery,需要HTML5的支持

    jQuery是现在最流行的JavaScript工具库. 据统计,目前全世界57.3%的网站使用它.也就是说,10个网站里面,有6个使用jQuery.如果只考察使用工具库的网站,这个比例就会上升到惊人的 ...

  5. python+selenium之字符串切割操作

    python+selenium之字符串切割操作 在Python中自带的一个切割方法split(),这个方法不带参数,就默认按照空格去切割字段,如果带参数,就按照参数去切割. 新建一个python文件, ...

  6. Docker入门与应用系列(九)图形界面管理之Portainer

    介绍 Portainer是一个开源.轻量级Docker管理用户界面,基于Docker API,可管理Docker主机或Swarm集群,支持最新版Docker和Swarm模式.官方文档https://p ...

  7. IOS学习笔记28—SQLite3第三方库之FMDB

    本文转载至 http://blog.csdn.net/happyrabbit456/article/details/11609451 SQLite是一种小型的轻量级的关系型数据库,在移动设备上使用是非 ...

  8. IOS控件:长度单位换算(UITextField,IBAction,委托事件touchesBegan)

    #import <UIKit/UIKit.h> @interface demo7_dayViewController : UIViewController { IBOutlet UITex ...

  9. 分布式服务框架:Zookeeper简介

    分布式服务框架:Zookeeper(分布式系统的可靠协调系统) 本文导读: 1 Zookeeper概述 2 Zookeeper总体结构 ——逻辑图.运转流程.特点.优点.数据结构 3 Zookeepe ...

  10. 【BZOJ4540】[Hnoi2016]序列 莫队算法+单调栈

    [BZOJ4540][Hnoi2016]序列 Description 给定长度为n的序列:a1,a2,…,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,…,a ...