gpt4 book ai didi

java - void 方法如何从调用堆栈中弹出?

转载 作者:行者123 更新时间:2023-12-03 23:19:36 24 4
gpt4 key购买 nike

这似乎是一个愚蠢的问题。我尝试了不同的资源来了解如何从调用堆栈中弹出 void 方法。我无法弄清楚,寻求帮助。提前致谢。

一个简单的例子——

public static void main(String args[]){
int sum = sum(2,3);
System.out.println(sum);
}
public static int sum(int first, int second){
return first + second;
}

因此,对于上面的示例调用堆栈将分配两个包含方法调用详细信息的堆栈帧,如下所示 -
enter image description here

最佳答案

来自 The Java® Virtual Machine Specification-Java SE 8 Edition [2.11.8] :

...the return instruction is used to return from methods declared to be void, instance initialization methods, and class or interface initialization methods.



因此,以下两种方法的字节码是相同的(用 BCV 查看),即单个 return操作说明:
public void test() {
return;
}

public void test() {
}

关于java - void 方法如何从调用堆栈中弹出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59617728/

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