这是我就读的大学的 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 useinvokeinterface
), static methods (which useinvokestatic
), and the few special cases handled byinvokespecial
.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/