Is Java evaluation order guaranteed in this case of method call and arguments passed in

前端 未结 2 576
傲寒
傲寒 2021-01-18 11:11

I did some reading up on JLS 15.7.4 and 15.12.4.2, but it doesn\'t guarantee that there won\'t be any compiler/runtime optimization that would change

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-18 11:31

    In addition to the argument evaluation order, the overview of the steps preformed when invoking a method, and their order, explained in section 15.12.4. Run-Time Evaluation of Method Invocation, makes it clear that all argument evaluations are performed before executing the method's code. Quote:

    At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument expressions are evaluated. Third, the accessibility of the method to be invoked is checked. Fourth, the actual code for the method to be executed is located. Fifth, a new activation frame is created, synchronization is performed if necessary, and control is transferred to the method code.

    The case you presented where an argument is only evaluated after control has been transferred to the method code is out of the question.

提交回复
热议问题