How does Java bind a variable name to a location in memory?

How does Java bind a variable name to a location in memory?

My first thought about this is that there should be a static table that is used for all variable names, which binds it to either its value or its location. Does this exist and is there a formal name for this table?

+4
source share
2 answers

The value of a variable of a primitive type is a number, and the value of a variable of a reference type is a reference (usually a memory address).

Now the question arises: where is the value of this variable stored. It depends on the type of variable - there are local variables, instance variables (fields) and class variables (static fields).

Local user names are resolved at compile time. Each variable becomes just the ith variable in the method, and it will be stored as the ith variable in the stack frame of some method call.

For example, the variables will be different. Field names are always present in bytecode (but, as a rule, are not present in machine codes generated by the JIT compiler). All objects of this class have the same layout, so the class can store the offset of the specified field - the distance from the beginning of the object. The interpreter can read the address of the object and add an offset to calculate where the variable is stored.

Class variables are similar to instance variables, but simpler. In this case, the class stores both the names and the values ​​of its variables.

+4
source

Historically, compilation involves creating a symbol table that associates variable names with their attributes defined from source code. This is a little simplified to illustrate, but the principles have not changed since FORTRAN was useful. Custom types in languages ​​such as C ++ and Java form part of the compilation unit metadata that is collected at compile time and tied together when the runtime executable is created or loaded into memory.

Note that all types must be defined before they can be used to define type objects. This is the goal of "import" in Java and "#include" in C / C ++. Metadata includes definitions of the methods and data elements of an object (or class) and is used to determine the size of objects to create in static memory, on the stack (block input / output), or heap (dynamic allocation).

Type checking at compile time or at runtime is one of the most significant events of the last forty years and is the main reason that we can use autonomous robots on Mars, on California highways or on the Internet. On this core, the central problem of compiling or translating a programming language traces everything that is known about objects and puts them in memory, where it can be used properly at runtime.

Ancient languages, such as FORTRAN and COBOL, had one type of variable (static), which would have only fundamental attributes of the data type. They had almost trivial symbol tables. The most difficult problem was to combine compilation units to execute. We have come a long way!

0
source

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


All Articles