I'm learning more about how to read Java bytecode, and it's very interesting. From my understanding, every stack frame gets its own array of variables. So, do all types (int
or a reference) share this same array?
public class ByteTest {
private int thisField;
public ByteTest(int f){
thisField = f;
}
}
In this code, iload_1
suggests that there is already a value in the 0th
spot, but f
is obviously the first int
variable in this block of code, except for the fact that this
is the first reference variable in this stack. So, all types share this same array? Am I correct in my reasoning? This is the bytecode for the constructor
/* L4 */
0 aload_0; /* this */
1 invokespecial 10; /* java.lang.Object() */
/* L5 */
4 aload_0; /* this */
5 iload_1; /* f */
6 putfield 13; /* .thisField */
/* L6 */
9 return;
Yes you are correct. A stack frame shares one set of local variables for all types. See also this section from the JVM specification:
Each frame (§2.6) contains an array of variables known as its local variables. The length of the local variable array of a frame is determined at compile-time and supplied in the binary representation of a class or interface along with the code for the method associated with the frame (§4.7.3).
A single local variable can hold a value of type
boolean
,byte
,char
,short
,int
,float
,reference
, orreturnAddress
. A pair of local variables can hold a value of typelong
ordouble
.