gpt4 book ai didi

java - java字节码中的局部变量

转载 作者:太空狗 更新时间:2023-10-29 23:02:54 25 4
gpt4 key购买 nike

我正在尝试学习 Java 字节码,我偶然发现了这个:我使用 -g 选项编译了这段非常简单的代码:

public class Test
{
public static void main(String args[])
{
double a = 1.0;
int b = (int)a;
}
}

主要代码原来是:

0 dconst_1
1 dstore_1
2 dload_1
3 d2i
4 istore_3
5 return

另外,main的最大局部变量是4个,而LocalVariableTable只有3个条目(args,a,b)。我很想知道为什么编译器保留了 4 个局部变量,而显然只有 3 个,并且 localvariable[2] 没有被使用。有什么我想念的吗?谢谢

最佳答案

是4是因为long和double在frame中占据了2个连续的entry。 Java JVM specification 明确指定了这一点:

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.

关于java - java字节码中的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1774996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com