updating references in an expression with a nested assignment

為{幸葍}努か 提交于 2019-12-05 04:03:45
ζ--

From JLS:

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.

This shows that in a.equals((a = null)). the following steps occur:

  1. a's reference is computed. This is sometimes called "binding the method call".
  2. a=null is evaluated as part of evaluating arguments. It does not affect step 1.
  3. The accessibility of equals(Object) is checked.
  4. JVM internal code finds the actual bytecode.
  5. Invocation occurs

Clearly, the reference of a is determined before a is nulled, averting an NPE.

I believe the section of the JLS you are interested in is

15.12.4. Run-Time Evaluation of Method Invocation

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

Thus, the evaluation of the target occurs while a still has a non-null value, THEN a is set to null as the argument to the method invocation. So the equals method is invoked with a this reference to the object formerly known as a, which still exists, with a null argument.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!