Given the instruction istore_<n> and its documentation:
n must be an index into the local variable array of the current frame (§2.6).
It does not specify what index it starts at. I assume 0. And for a given istore operation it should increment by one.
Given a simple class:
public class TestingStuff {
public static void main(String[] args) {
int a = 11;
int b = 12;
}
public static void test() {
int c = 13;
int d = 14;
}
}
I would expect the two methods to have different frames. Which then should mean the instruction for storing a and b would be istore_0 and istore_1. And the same index for storing c and d. But for some reason, the index in the main method starts at 1. This seems like it is always the case. But I can't find any information on why.
Output from javap:
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=3, args_size=1
0: bipush 11
2: istore_1
3: bipush 12
5: istore_2
6: return
public static void test();
descriptor: ()V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=2, args_size=0
0: bipush 13
2: istore_0
3: bipush 14
5: istore_1
6: return
Is it the case? If so, why?
The parameters to a static method are passed in as local variables starting at position 0. Therefore, for main, the args array is in the local variable at position 0. Compiling code that does something with args should demonstrate that, though I don't have a compiler handy.
(For instance methods, this is at position 0, and then the remaining arguments start at position 1.)
See section 2.6.1 for details.