具有链接方法的Java方法调用顺序 [英] Java method invocation order with chained methods

查看:53
本文介绍了具有链接方法的Java方法调用顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下Java代码示例:

Given is the following Java code example:

builder.something()
       .somethingElse()
       .somethingMore(builder.getSomething());

Java语言规范是否保证在 somethingElse()方法后的之后调用 getSomething(),还是允许Java实现?重新排列执行顺序?

Is it guaranteed by the Java Language Specification that getSomething() is invoked after the somethingElse() method or is a Java implementation allowed to reorder the execution?

推荐答案

The JLS, Section 15.12.4, guarantees that the target reference is computed before arguments are evaluated.

在运行时,方法调用需要五个步骤.首先,可以计算目标参考.其次,对参数表达式进行求值....

At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument expressions are evaluated. ...

必须首先评估 somethingElse 方法,以计算 somethingMore 方法的目标引用.然后对 builder.getSomething()进行评估,以为 somethingMore 提供参数值.然后,可以执行 somethingMore .

The somethingElse method must be evaluated first, to compute the target reference for the somethingMore method. Then builder.getSomething() is evaluated to supply a value for the parameter to somethingMore. Then somethingMore can be executed.

由于此规则,不允许JVM对执行重新排序.

Because of this rule, JVMs are not allowed to reorder the execution.

这篇关于具有链接方法的Java方法调用顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆