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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html
So instead of saving a whole string in each
getfieldinstruction 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/