Dan Webster
Dan Webster

Reputation: 1253

How does Java associate a variable name with a location in memory?

How does Java associate a variable name with a location in memory?

My first thought about this is there must be a static table that is used for all variable names which associates it with either it's value or a location to it's value. Does it exist and is there a formal name for this table?

Upvotes: 4

Views: 2314

Answers (2)

user1295785
user1295785

Reputation:

Historically compilation includes the creation of a Symbol Table which associates variable names with their attributes determined from the source code. This is a little simplistic for purposes of illustration, but the principles have not changed since FORTRAN was useful. User-defined types in languages such as C++ and Java form part of each compilation unit's meta-data that is collected during compilation and knitted together when a run-time executable is created or loaded into memory.

Note that all of the types must be defined before they may be used to define objects of a type. This is the purpose of 'import' in Java and '#include' in C/C++. The meta-data includes definitions of methods and object (or class) data elements and is used to determine the size of objects for creation in static memory, on the stack (block entry/exit) or the heap (dynamic allocation).

Type-checking at compile time or during execution is one of the most significant developments in the last forty years and is a major reason that we are able to use autonomous robots on Mars, on California highways or on the Internet. At it's core, the central problem of programming language compilation or translation is tracking everything known about objects and placing it in memory where it can be used properly at run-time.

Ancient languages like FORTRAN and COBOL had one type of variable (static) that would have only fundamental data type attributes. They had almost trivial symbol tables. The most complex issue was linking compilation units together for execution. We've come a long way Baby!

Upvotes: 0

zch
zch

Reputation: 15278

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

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

Names of locals are resolved during compilation. Each variable becomes simply i-th variable in method and it will be stored as i-th variable in stack frame of some method call.

For instance variables it will be different. Field names are always present in bytecode (but will generally not be present in machine code generated by JIT compiler). All objects of given class have the same layout, so class can store offset of given field - distance from beginning of the object. Interpreter can read the address of object and add offset to calculate where is variable stored.

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

Upvotes: 5

Related Questions