Wednesday, April 25, 2012


I'm still going to work on the whole bytecode thing, that should be up tomorrow or the day after.

Anyways, I want to make some sort of 2D game in Java (I get bored often).
I was thinking of asking you guys what sort of game you would like to see:

  • Mario-type platformer?

  • 2D side-scrolling shooter?

  • Space Invaders-type shooter?

  • Something different?

It's up to you guys.

Every once in a while, I will post the current version of the game, to keep my audience entertained!

Let me know what you guys want me to make! I will most likely follow this up with some poll (or not) to determine the winner.

Sunday, April 22, 2012

Java Bytecode: Lesson 3

Today we are going to look at a real example of bytecode, a "Hello, world" program.

public class Example {
int number = 4;
public Example() {
System.out.println("Hello, world!");
System.out.println("Number: " + number);
public static void main(String[] args) {
new Example();

All this really does is initialize the Example class, printing Hello, world! and Number: 4.

Now, what happens when we disassemble this (after compiling this of course)?
$ javap -c Example
Compiled from ""
public class Example extends java.lang.Object{
int number;

public Example();
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: aload_0
5: iconst_4
6: putfield #2; //Field number:I
9: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
12: ldc #4; //String Hello, world!
14: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
17: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
20: new #6; //class java/lang/StringBuilder
23: dup
24: invokespecial #7; //Method java/lang/StringBuilder."":()V
27: ldc #8; //String Number:
29: invokevirtual #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
32: aload_0
33: getfield #2; //Field number:I
36: invokevirtual #10; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
39: invokevirtual #11; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
42: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
45: return

public static void main(java.lang.String[]);
0: new #12; //class Example
3: dup
4: invokespecial #13; //Method "":()V
7: pop
8: return


At this point, you will start to see classes, methods, line numbers (these don't relate directly to the source code), and of course, bytecodes. On the right of some of these bytecodes, the numbers relate to the values in the constant pool.
The constant pool stores many things, such as numbers, class names, signatures, string, all kinds of things.

Now, lets explore the very first line of the program, in the main method:
public static void main(java.lang.String[]);
0: new #12; //class Example
3: dup
4: invokespecial #13; //Method "":()V
7: pop
8: return

Line 0 creates a new object determined by the two bytes following the instruction (that's why the next instruction is on line 3 -- 0, 1 and 2 are taking up space), which correspond to the constant pool. In this case, the bytes are referring to the twelfth constant in the constant pool, a class by the name of Example (sounds familiar, right).
Line 3 simply duplicates the new instance of this class, therefore there are now two of the same objects on the top of the stack. This is done so we can refer to the object later on, as opposed to losing it right as we call the method.
Line 4 uses the object on the top of the stack and two other bytes (once again, referring to the constant pool), referring to the method (the public constructor), and invokes that instance method (only accessible from the class itself). It pops the instance of the class, any arguments (in this case, none) and basically moves to the first instruction in the method to be called. This will return whatever is returned from the method, but...
Line 7 pops the top-most part of the stack off. This means that anything that was returned is now gone, but that's good, there was nothing we needed to return!
Line 8, you guessed it, returns from the method. That's it.

Next post we are going to look at the Example constructor in the same manner.
See you soon!

Saturday, April 21, 2012

Java Bytecode: Lesson 2

Each of the Java bytecodes fall into a few categories, here they are, along with examples:

  • Loading and Storing (aload_0, astore_0)

  • Logic and Arithmetic (dcmpg, iadd)

  • Type Conversion (i2d)

  • Object Creation and Manipulation (new, putfield)

  • Stack Management (swap)

  • Control Transfer (goto)

  • Method Invoking and Returning (invokestatic, areturn)

  • And of course, nothing (nop)

Most bytecodes are also prefixed by the initial object type:

  • Integer (i)

  • Long (l)

  • Short (s)

  • Byte (b)

  • Character (c)

  • Float (f)

  • Double (d)

  • Reference (a)

- Wikipedia article on Java bytecode.

For further information on each specific bytecode, you can look at this Wikipedia article.

Next time I will dissect the bytecode of such a simple program in any language, the "Hello world" example.

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.

Wednesday, April 18, 2012

Hello, world!

Hello everybody.
This will be my future blog relating to teaching people about Java bytecode.

What is bytecode?
Java bytecode is the form of instructions that the Java virtual machine executes. Each bytecode opcode is one byte in length, although some require parameters, resulting in some multi-byte instructions. Not all of the possible 256 opcodes are used. 51 are reserved for future use. Beyond that, Sun Microsystems, the original creators of the Java programming language, the Java virtual machine and other components of the Java Runtime Environment (JRE), have set aside three values to be permanently unimplemented. (

If you don't quite understand what I mean, leave a comment and I will attempt to explain a bit more.
Hope to see y'all and more followers later on!