Friday, April 20, 2012

Java Bytecode: Lesson 1

In order to start learning about Java bytecode, you first need to understand one key thing - the stack.

A stack is sort of like... a stack of plates. In order to use a plate, you always grab one from the top of the stack; likewise, in order to store a plate, you always put one on the top. You're not some sort of magician either, sorry; you are always going to use the top-most part of the stack. The Java Virtual Machine, which runs the Java bytecode is stack-oriented.

- Wikipedia article on stacks

Most bytecodes use the top-most part of the stack as arguments. I will explain each type of bytecode more thoroughly, but for now I'm going to use the bytecodes iload_0 (pushes local variable 0 onto the stack) and ineg (negative integer). After calling both of these, local variable 0 (which we will assume is 5) will be pushed onto the stack from temporary storage and the 5 will be turned from 5 to -5 from the top of the stack.

If you can't quite grasp the concept yet, look at the Wikipedia article on stacks, or leave a question.

Before this instruction, you had a positive 5 on the top of the stack, after the instruction, you now have a negative 5 on the top of the stack. This is how most bytecodes work. There are some that require other byte values, such as iload, which uses the other byte to put the appropriate object on the top of the stack.

More explanation over these bytecodes will come soon. If you have any questions, don't hesitate to leave a question below (or any other comments, advice, if I wrote something incorrectly, etc), I will happily reply!

Expect another post soon.

No comments:

Post a Comment