Tito
Tito

Reputation: 9054

Understanding bytecode generated for a simple java class

I am following this blog to study java to bytecode & I have generated bytecode for this SimpleClass.

public class SimpleClass {

    public int simpleF = 5;


}

I understand the bytecode positions as below

But I do not understand aload_0 at position 4 and its purpose ?

// Compiled from SimpleClass.java (version 1.6 : 50.0, super bit)
public class SimpleClass {

  // Field descriptor #6 I
  public int simpleF;

  // Method descriptor #8 ()V
  // Stack: 2, Locals: 1
  public SimpleClass();
     0  aload_0 [this]
     1  invokespecial java.lang.Object() [10]
     4  aload_0 [this]
     5  iconst_5
     6  putfield SimpleClass.simpleF : int [12]
     9  return
      Line numbers:
        [pc: 0, line: 2]
        [pc: 4, line: 4]
        [pc: 9, line: 2]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: SimpleClass
}

Upvotes: 1

Views: 267

Answers (2)

Mutaz Alghafary
Mutaz Alghafary

Reputation: 43

For the JVM to execute almost anything it must push things into the execution stack , aload_0 is used to load an object from the local variables array (position 0 ) to the execution stack , for instance methods position 0 always refers to this which is a reference to the current object .

More information can be found in this article : http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne.html

Upvotes: 0

user2543740
user2543740

Reputation: 179

aload_0 is there to push in the stack the reference the current class in the statement that set the local non-static field simpleF to 5.

[this.]simpleF=5

From http://cs.au.dk/~mis/dOvs/jvmspec/ref-putfield.html

putfield sets the value of the field identified by <field-spec> in objectref (a reference to an object) to the single or double word value on the operand stack.

Upvotes: 2

Related Questions