How does the java compiler assign an index to a local variable table?

Ok, so I am writing a compiler, and I am trying to use the information in the local variable table to define names / types for variables.

I have the following code:

public void noob() {
    try {
        int hello = 0;
        short yo = 1;
        byte y = 2;
        int[] e = new int[9];

        System.out.println(y + ", " + hello + ", " + yo+", "+e);

    } catch (Exception var6) {
        var6.printStackTrace();
    }
}

When reading the variable table, I get the following:

LocalVariable{uid=-1, start=0, end=69, nameIndex=30, typeIndex=31, varIndex=0, name='this', typeName='LMain;'}
LocalVariable{uid=-1, start=2, end=60, nameIndex=37, typeIndex=18, varIndex=1, name='hello', typeName='I'}
LocalVariable{uid=-1, start=4, end=60, nameIndex=38, typeIndex=39, varIndex=2, name='yo', typeName='S'}
LocalVariable{uid=-1, start=6, end=60, nameIndex=40, typeIndex=41, varIndex=3, name='y', typeName='B'}
LocalVariable{uid=-1, start=12, end=60, nameIndex=42, typeIndex=43, varIndex=4, name='e', typeName='[I'}
LocalVariable{uid=-1, start=64, end=68, nameIndex=44, typeIndex=45, varIndex=1, name='var6', typeName='Ljava/lang/Exception;'}

(ignore uid column).

I noticed that varIndex is not unqiue for every variable. Why do hello (int)they var6(Exception)use the same varIndex?

+4
source share
1 answer

, , . , .

hello var6 , .

+6

Source: https://habr.com/ru/post/1625251/


All Articles