java - 程序是在调用的方法完成后终止,还是将控制权返回给调用函数?

标签 java

这是我就读的大学的 Java 类(class)中的一个问题。我的老师说答案是 D——“程序终止”,但我认为答案是 C——“控制返回到方法 C”。

正确答案是什么,为什么?

If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens?

A. Control is returned to method A

B. Control is returned to method B

C. Control is returned to method C

D. The program terminates

最佳答案

答案是c,除非方法D导致程序终止,否则答案是d。

方法调用的行为在invokevirtual操作码(操作码)的定义中被明确定义。来自 Java Virtual Machine Online Instruction Reference :

invokevirtual dispatches a Java method. It is used in Java to invoke all methods except interface methods (which use invokeinterface), static methods (which use invokestatic), and the few special cases handled by invokespecial.

For example, when you write in Java:

Object x;
...
x.equals("hello"); 

this is compiled into something like:

aload_1       ; push local variable 1 (i.e. 'x') onto stack
ldc "hello"   ; push the string "hello" onto stack

; invoke the equals method
invokevirtual java/lang/Object/equals(Ljava/lang/Object;)Z
; the boolean result is now on the stack

Once a method has been located, invokevirtual calls the method. (...) When the method called by invokevirtual returns, any single (or double) word return result is placed on the operand stack of the current method and execution continues at the instruction that follows invokevirtual in the bytecode.

关于java - 程序是在调用的方法完成后终止,还是将控制权返回给调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260699/

相关文章:

Java 服务器在使用 Javafx 单击按钮时停止

java - 同样的代码,同样的输入,有时跑得快,有时跑得慢,为什么?

java - 将支持库更新到 23.2.1 后 TabLayout 崩溃

Java SQL Derby Getconnection非法修饰符

java - 带有 ServletContextListener 的线程池

java - 从另一台PC连接到MySQL服务器(从java应用程序)

java - 如何使用作为 uberjar 部署的库?

java - Tomcat 7.0 服务启动失败,出现 ClassNotFoundException : javax. ws.rs.ProcessingException

java - Kotlin/Java : Add multiple items to a Builder class that only allows one . 一次 add()

java - 从脚本启动 Spring boot 应用程序