JVM如何实现变量名到内存地址的映射?附代码示例咨询
height and name Great question—this digs into the core of how JVM and compilers bridge human-readable code with low-level memory operations, and it’s way more nuanced than a simple "lookup table" might suggest. Let’s break this down step by step.
First, Your Guess: Is There a Mapping Table in the Method Area?
You’re on the right track, but the details are a bit different. The JVM doesn’t store a direct map of variable names to memory addresses in the method area—because every instance of Cat has its own unique memory address, so fixed addresses wouldn’t work. Instead, when the Cat class is loaded:
- The JVM stores metadata about the class in the method area, including a list of all fields (
height,name, etc.). - Each field is assigned a memory offset—a fixed number of bytes from the start of any
Catinstance. This offset is what gets used to calculate the actual memory address at runtime.
How the JVM Handles cat.height = 100
Let’s walk through exactly what happens when your code runs:
Compilation Phase
When you compile your Java code, the compiler turnscat.height = 100into bytecode instructions. The key one here isputfield, which references theheightfield using a symbolic reference (likeCat.height’s fully qualified name, stored in the class file’s constant pool).Class Loading Phase
Before your code runs, the JVM loads theCatclass into memory. During this process, it resolves the symbolic references in the bytecode to concrete metadata:- It assigns
heighta fixed offset (say, 16 bytes from the start of aCatinstance) andnameanother offset (maybe 20 bytes, depending on object headers and field types). - This metadata lives in the method area, tied to the
Catclass.
- It assigns
Runtime Execution
Whencat.height = 100executes:- The JVM first retrieves the memory address of the
Catinstance thatcatreferences (this address is stored in the local variable table for the current method). - It adds the precomputed offset for
heightto this base address to get the exact memory location whereheightis stored for this instance. - Finally, it writes the value
100into that memory location.
- The JVM first retrieves the memory address of the
Why "Variable Names" Don’t Exist for the Computer
Here’s the big realization: computers never "recognize" variable names—those are purely a convenience for humans. Here’s what actually happens:
- For instance fields (like
heightandname), variable names are converted to symbolic references during compilation, then resolved to offsets during class loading. At runtime, only offsets and base object addresses are used. - For local variables (like
Cat catin your code), the compiler assigns them an index in the method’s local variable table. The JVM uses these indexes to access the values (in this case, theCatinstance reference) instead of any name.
To make this concrete, here’s a simplified snippet of what the bytecode for your code might look like:
// Cat cat = new Cat(); new #1 // Create new Cat instance, push reference to stack dup // Duplicate the reference invokespecial #2 // Call Cat's constructor astore_1 // Store the reference in local variable table index 1 (cat) // cat.height = 100; aload_1 // Load cat reference from local variable index 1 bipush 100 // Push 100 to stack putfield #3 // Write 100 to Cat's height field (resolved to offset at load time) // cat.name = "Tom"; aload_1 // Load cat reference ldc #4 // Push the string "Tom" from constant pool putfield #5 // Write to Cat's name field
Wrapping Up
Your initial intuition about a lookup mechanism was correct, but the JVM uses offsets (not fixed addresses) because object instances are scattered across the heap. Variable names are just a human-friendly layer that gets stripped away long before the computer executes the code.
内容的提问来源于stack exchange,提问作者Bonsaisteak

