Arvin Arvin - 5 years ago 102
Java Question

Reading a Java bytecode instruction: What does the number mean?

I was reading java bytecode and saw this:

getfield #5 (Field java.lang.String name)


What does
#5
mean?

And how can I write a program in bytecode?

Answer Source

Java class files and bytecode

Java class files (bytecode-files) is composed by different components:

http://en.wikipedia.org/wiki/Java_class_file

  • Magic Number: 0xCAFEBABE
  • Version of Class File Format: the minor and major versions of the class file
  • Constant Pool: Pool of constants for the class
  • (...)
  • Fields: Any fields in the class
  • Methods: Any methods in the class
  • Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)

The number #5 simply refers to a location in the constant pool. And in that position a CONSTANT_FieldRef is found which contains a reference to a CONSTANT_NameAndType among other attributes. And CONSTANT_NameAndType contains a reference to a CONSTANT_Utf8 (which contains the actual string/name.)

So the flow looks like this:

getfield #number -> FieldRef -> NameAndType -> Utf8 -> string

http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html

So instead of saving a whole string in each getfield instruction a number is saved. This improves performance in the interpreter (or JIT) and space in the class file.

Hand-write bytecodes

Hand-written bytecodes can be assembled to a class file with this tool (it contains a lot of examples):

http://jasmin.sourceforge.net/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download