Java Reference
In-Depth Information
publicintsquare(int);
Code:
Stack=2,Locals=2,Args_size=2
0:iload_1
1:iload_1
2:imul
3:ireturn
}
We cannot run this program because there is no main() method. But this is not our purpose;
we simply wish to look at some code.
Now, the first line is the class header:
publicclassSquareextendsjava.lang.Object
It tells us the name of the class is Square and its super class is java.lang.Object . The
next two lines tell us something about the version of the JVM that we are using; we are not
interested in this. Following this is a constant pool, which in this case defines eleven con-
stants. The numerical tags #1 ,. . . , #11 are used in the code for referring to these constants.
For example, #10 refers to the method name square ; #11 refers to its descriptor (I)I 1 . The
use of the constant pool may appear somewhat archaic, but it makes for a compact class
le.
Notice there are two methods defined. The first is a constructor.
publicSquare();
Code:
Stack=1,Locals=1,Args_size=1
0:aload_0
1:invokespecial#8;//Methodjava/lang/Object."<init>":()V
4:return
This constructor (generated as a method named <init> ) is the implied empty constructor
(having no arguments), which the compiler must generate if the user has not defined an
explicit version. The first line of this code is not really code at all but rather a sequence of
values that the run-time JVM will want to know when it allocates a stack frame:
Stack=1,Locals=1,Args_size=1
The Stack=1 indicates that one memory location is required for partial results (in this case,
for loading this onto the stack). The Locals=1 says just one local variable is allocated in
the stack frame, in this instance, the location at offset 0 for this . The Args_size=1 says
there is just one actual argument to this constructor (in this case, the implicit argument,
this ).
Then there is the code, proper:
0:aload_0
1:invokespecial#8;//Methodjava/lang/Object."<init>":()V
4:return
At location 0 is a one-byte aload_0 instruction that loads this onto the stack. Following
that, at location 1, is a three-byte invokespecial instruction. The first byte holds the
opcode for invokespecial , and the next two bytes hold a reference to the constant named
#8 in the constant pool: Method#4.#7; . This constant in turn makes reference to additional
constant entries #4 and #7 . Taken as a whole, the invokespecial instruction invokes the
1 Recall, adescriptordescribes a method's signature. In this case, (I)I describes a method that takes
one int as an argument and returns an int result.
 
Search WWH ::




Custom Search