Java - 方法调用和执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2486885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:25:53  来源:igfitidea点击:

Java - Method Invocation & Execution

javaterminology

提问by Paul

What is the difference between the invocation and execution of a method ? Are two the same thing ?

方法的调用和执行有什么区别?两个是一回事吗?

回答by Bozho

I don't think these are standard terms. However I understand them the following way:

我不认为这些是标准术语。但是我通过以下方式理解它们:

  • invocationis the event of issuing the call to the method; technically - placing the method onto the stack
  • executionis the whole process of running the method - from invocation till completion. And execution timeis period during which the method body runs.
  • 调用是发出方法调用的事件;从技术上讲 - 将方法放到堆栈上
  • 执行是运行方法的整个过程——从调用到完成。和执行时间是在此期间,该方法运行体周期。

回答by Halo

Well, invoking a method means calling it by its name and parameters; executing a method means executing it.. running it, fetching its lines one by one and run them.

好吧,调用一个方法意味着通过它的名字和参数来调用它;执行一个方法意味着执行它..运行它,一一获取它的行并运行它们。

回答by Joachim Sauer

I'm not aware of any standard definitions of those, but my understanding is this:

我不知道这些的任何标准定义,但我的理解是:

  • invocation is the act of calling a method
  • execution is the act of actually running the method
  • 调用是调用方法的行为
  • 执行是实际运行方法的行为

Invocation results in execution.

调用导致执行。

回答by Jothsna Nalla

As far as my knowledge is concern:

据我所知:

Invocation is the pre-step for execution. If invocation is successful then the process of execution starts...

调用是执行的前置步骤。如果调用成功,则执行过程开始......

For example,

例如,

Parameters (the variables declared in the method signature) will be created only during method invocation.It is the pre-step for execution. After the invocation, the actual method will be executed i.e., the local variables(the variables which are declared in the method body) will be created during the method execution.

参数(方法签名中声明的变量)只会在方法调用期间创建。它是执行的前一步。调用后,将执行实际的方法,即在方法执行期间创建局部变量(在方法体中声明的变量)。

so parameters are at invocating and local variables are at executing...

所以参数在调用和局部变量在执行...

Thus, The successful invocation leads to proceed to execution.

因此,成功的调用导致继续执行。

回答by polygenelubricants

There are some subtle differences:

有一些细微的区别:

  • Context
    • An invocation context is associated with the caller
      • e.g. the parameters you're using to invoke a method are the actual parameters
    • An execution context is associated with the callee
      • e.g. the parameters you're using in a method execution are formal parameters
  • Dynamic dispatch
    • A method invokation can lead to the execution of any one of many methods
    • An executing method is precisely one executing method
  • Order: invocation precedes execution
    • Invocation of a method doesn't immediately start its execution
      • Imagine if the method is remote
      • Invocation failure could be caused by broken connection, error in handling the arguments over the wire, etc
    • A method only starts executing after invocation is successful
  • 语境
    • 调用上下文与调用者相关联
      • 例如,您用来调用方法的参数是实际参数
    • 执行上下文与被调用者相关联
      • 例如,您在方法执行中使用的参数是形式参数
  • 动态调度
    • 方法调用可以导致执行多种方法中的任何一种
    • 一种执行方法正是一种执行方法
  • 顺序:调用先于执行
    • 方法的调用不会立即开始执行
      • 想象一下如果方法是远程的
      • 调用失败可能是由断开的连接、处理网络上的参数错误等引起的
    • 方法只有在调用成功后才开始执行

See also: Overview of Remote Method Invocation. When you consider the method to be remote, the difference between invocation (a request to start the execution of something) and execution (something that is happening somewhere if the request is successful) becomes more apparent.

另请参阅:远程方法调用概述。当您认为该方法是远程的时,调用(请求开始执行某事)和执行(如果请求成功则在某处发生的某事)之间的区别变得更加明显。

Consider also the case with reflection. This is a method of java.lang.reflect.Method:

还要考虑反射的情况。这是一种方法java.lang.reflect.Method

public Object invoke(Object obj, Object... args) throws
  IllegalAccessException,   // failure during invocation
  IllegalArgumentException, // failure during invocation
  InvocationTargetException // invocation was successful,
                               // but exception was thrown during execution

Here also clearly invocation and execution are two different things. If you need more convincing, consider the case of an invocation vs execution NullPointerExceptionin this reflection context:

这里也清楚地调用和执行是两件不同的事情。如果您需要更有说服力,请考虑NullPointerException在此反射上下文中调用与执行的情况:

  • It can be thrown during invocation, when obj == nullwhen the method is an instance method
  • It can be thrown during execution, in which case it will be wrapped as the causeof an InvocationTargetException
  • obj == null当方法是实例方法时,它可以在调用期间抛出
  • 它可以在执行过程中被抛出,在这种情况下,将被包装为原因InvocationTargetException