Confusion about the size of references in the JVM spec

前端 未结 1 1777
旧巷少年郎
旧巷少年郎 2021-01-22 11:03

The JVM spec states that references only take up one local variable slot( jvms12 2.6.1). Additionally it states that double and long, take up two local

相关标签:
1条回答
  • 2021-01-22 11:49

    Does this mean that all JVM compliant implementations must use 32-bit addressing? How do 64-bit JVMs handle this? Do they use 64 bit local variable slots, or do they use 2 slots for references?

    No.

    The slots are an abstraction whose purpose is to allow the behavior of the bytecodes to be specified. The JVM interpreter and JIT compiler do some clever things to map the slots to virtual memory addresses. These mappings take account of the fact that a reference may be a 32 or 64 bit address (or a 32 bit compressed OOP)

    One (abstract) slot is used for a reference irrespective of the actual size of pointers.

    Q: Why did they define slots like this?

    A: So that the same bytecodes have the same meaning on 32 and 64 bit Java platforms!

    Q: Why doesn't it fall apart if you try to look at the contents a slot as a different type?

    A: Because the JVM's bytecode analyser won't let you do that!

    0 讨论(0)
提交回复
热议问题