Java - 方法调用和方法执行

发布于 2024-08-26 04:19:18 字数 31 浏览 3 评论 0原文

方法的调用和执行有什么区别?两个是同一个东西吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

向日葵 2024-09-02 04:19:18

我不认为这些是标准术语。不过我对它们的理解如下:

  • 调用是发出对方法的调用的事件;从技术上讲 - 将方法放入堆栈
  • 执行是运行方法的整个过程 - 从调用到完成。 执行时间是方法体运行的时间段。

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

  • invocation is the event of issuing the call to the method; technically - placing the method onto the stack
  • execution is the whole process of running the method - from invocation till completion. And execution time is period during which the method body runs.
再浓的妆也掩不了殇 2024-09-02 04:19:18

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

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.

前事休说 2024-09-02 04:19:18

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

  • 调用是调用方法的行为,
  • 执行是实际运行方法的行为,

调用导致执行。

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.

如果没有 2024-09-02 04:19:18

有一些细微的差别:

  • 上下文
    • 调用上下文与调用者关联
      • 例如,您用来调用方法的参数是实际参数
    • 执行上下文与被调用者关联
      • 例如,您在方法执行中使用的参数是形式参数
  • 动态调度
    • 方法调用可以导致执行多种方法中的任何一种
    • 一个执行方法就是一个执行方法
  • 顺序:调用先于执行
    • 方法的调用不会立即开始执行
      • 想象一下如果该方法是远程的
      • 调用失败可能是由于连接断开、处理线路参数时出错等原因
    • 方法只有在调用成功后才开始执行

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

还要考虑反思的情况。这是 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

这里也明确了调用和执行是两个不同的东西。如果您需要更有说服力,请考虑此反射上下文中调用与执行 NullPointerException 的情况:

  • 它可以在调用期间抛出,当 obj == null 当方法是实例方法
  • 它可以在执行期间抛出,在这种情况下,它将被包装为 InitationTargetException原因

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:

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 NullPointerException in this reflection context:

  • It can be thrown during invocation, when obj == null when the method is an instance method
  • It can be thrown during execution, in which case it will be wrapped as the cause of an InvocationTargetException
挖鼻大婶 2024-09-02 04:19:18

据我所知:

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

例如,

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

所以参数在调用,局部变量在执行……

这样,调用成功就会继续执行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文