有效的不可用常量池索引

Valid unusable constant pool index

我可以在 JVM 文档中阅读以下内容:

All 8-byte constants take up two entries in the constant_pool table of the class file. If a CONSTANT_Long_info or CONSTANT_Double_info structure is the item in the constant_pool table at index n, then the next usable item in the pool is located at index n+2. The constant_pool index n+1 must be valid but is considered unusable.

In retrospect, making 8-byte constants take two constant pool entries was a poor choice.

如果索引n+1有效,怎么会是"unusable"呢?此外,它似乎并没有完全无法使用,因为可以在其中存储一个值:

A value of type long or type double occupies two consecutive local variables. Such a value may only be addressed using the lesser index. For example, a value of type double stored in the local variable array at index n actually occupies the local variables with indices n and n+1; however, the local variable at index n+1 cannot be loaded from. It can be stored into. However, doing so invalidates the contents of local variable n.

是否意味着"valid" = "you can store into it", "unusable" = "you can't load it"?

您混淆了常量池和局部变量。

第一个cite讲的是常量池,顾名思义就是常量。你不能存储到常量池中。短语“constant_pool 索引 n+1 必须有效”意味着,例如,如果池的最后一个条目是 longdouble 常量,您仍然必须声明池足够大以容纳两个条目,尽管第二个条目不可用。我不知道任何其他实际后果,所以它主要是一个正式的声明,暗示有一个连续的有效索引范围,尽管其中一些无法读取。

你第二次引用的局部变量是另一回事。局部变量是可以写入的,写入的值不需要与之前的类型相同,唯一的要求是后续读取适合变量的当前类型,即最近写入的值。所以你可以写入索引 n+1 即使之前 longdouble 类型的值在 n,尽管这意味着之后 n 处没有有效值,只有 n+1.

处的新值