问题
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 variable slots because they are 64-bit.
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?
回答1:
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!
来源:https://stackoverflow.com/questions/57909315/confusion-about-the-size-of-references-in-the-jvm-spec