You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JVM如何实现变量名到内存地址的映射?附代码示例咨询

How JVM Resolves Field Addresses Like 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 Cat instance. 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:

  1. Compilation Phase
    When you compile your Java code, the compiler turns cat.height = 100 into bytecode instructions. The key one here is putfield, which references the height field using a symbolic reference (like Cat.height’s fully qualified name, stored in the class file’s constant pool).

  2. Class Loading Phase
    Before your code runs, the JVM loads the Cat class into memory. During this process, it resolves the symbolic references in the bytecode to concrete metadata:

    • It assigns height a fixed offset (say, 16 bytes from the start of a Cat instance) and name another offset (maybe 20 bytes, depending on object headers and field types).
    • This metadata lives in the method area, tied to the Cat class.
  3. Runtime Execution
    When cat.height = 100 executes:

    • The JVM first retrieves the memory address of the Cat instance that cat references (this address is stored in the local variable table for the current method).
    • It adds the precomputed offset for height to this base address to get the exact memory location where height is stored for this instance.
    • Finally, it writes the value 100 into that memory location.

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 height and name), 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 cat in 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, the Cat instance 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:15:27