[java] Slots in the java local variable table are reusable

insert image description here

1 Overview

2. Local variable table

The Local Variables Table is used to store the local variables in the method, as well as the method parameters. 当 Java 源代码文件被编译成 class 文件的时候,局部变量表的最大容量就已经确定了.

Let's look at such a piece of code.

public class LocalVaraiablesTable {
    
    
    private void write(int age) {
    
    
        String name = "沉默王二";
    }
}

The write() method has a parameter age and a local variable name.

Then use Intellij IDEA's jclasslib to view the compiled bytecode file LocalVariablesTable.class. You can see that in the Code property of the write() method, Maximum local variablesthe value of (the maximum capacity of the local variable table) is 3.

insert image description here
It stands to reason that the maximum capacity of the local variable table should be 2, one age, one name, why is it 3?

When a member method (non-static method) is called, the 第 0 个变量其实是调用这个成员方法的对象引用,也就是那个大名鼎鼎的 this。method write(18) is called, which is actually called write(this, 18).

Click on the Code property and check LocalVaraiableTableto see the detailed information.

insert image description here
The 0th is this, the type is LocalVariablesTable object; the first is the method parameter age, the type is the integer int; the second is the local variable name inside the method, the type is the string String.

Of course, the size of the local variable table is not the sum of all the local variables in the method, it is related to the type of the variable and the scope of the variable. When the scope of a local variable ends, its place in the local variable table is replaced by the next local variable.

Take a look at the code below.

public static void method() {
    
    
    // ①
    if (true) {
    
    
        // ②
        String name = "沉默王二";
    }
    // ③
    if(true) {
    
    
        // ④
        int age = 18;
    }
    // ⑤
}
  • method() 方法的局部变量表大小为 1, because it is a static method, there is no need to add this as the first element of the local variable table;

  • ②When the local variable has a name, the size of the local variable table becomes 1;

  • ③ When the scope of the name variable ends;

  • ④ When the local variable has an age, the size of the local variable table is 1;

  • At ⑤, the scope of the local age variable ends;

Regarding the scope of local variables, Advice 57 in Effective Java:

Minimizing the scope of local variables increases code readability and maintainability, and reduces the chance of errors.

Here, I have one more point to remind you. 为了尽可能节省栈帧耗用的内存空间,局部变量表中的槽是可以重用的, as demonstrated by the method() method, which means that reasonable scoping can help improve program performance.

The capacity of the local variable table is based on the slot (slot) as the smallest unit, 一个槽可以容纳一个 32 位的数据类型(for example, int, of course, the "Java Virtual Machine Specification" does not clearly indicate the size of the memory space that a slot should occupy, but I think it is easier to understand),像 float 和 double 这种明确占用 64 位的数据类型会占用两个紧挨着的槽。

Take a look at the code below.

public void solt() {
    
    
    double d = 1.0;
    int i = 1;
}

As you can see with jclasslib, the value of the Maximum local variable of the solt() method is 4.

insert image description here
Why is it equal to 4? Bringing this is also 3?

insert image description here
Looking at the LocalVariiableTable, you can see that the subscript of the variable i is 3, which means that the variable d occupies two slots.

insert image description here
At this time, the knowledge is linked, and I like [Java] String and StringTable ashes to explain the source code in detail . There are String slots in it.

M. Reference

Reprinted: Ctrip interviewer asked me about the Java virtual machine stack

Guess you like

Origin blog.csdn.net/qq_21383435/article/details/123647894