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?
Java class files and bytecode
Java class files (bytecode-files) is composed by different components:
http://en.wikipedia.org/wiki/Java_class_file
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):