Looking at this example code similar to this question:
public class A {
public static void main(String args[]){
A a = new A();
System.out.println(a.equals((a = null)));
}
}
This prints false. Why doesn't it fail with a NullPointerException? The assignment has to get processed before the equals method can run, but somehow that doesn't affect the reference that equals is called on until after the whole line is evaluated?
I didn't see where in the Java language spec it describes this, did I miss it somewhere?
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:
a
's reference is computed. This is sometimes called "binding the method call".a=null
is evaluated as part of evaluating arguments. It does not affect step 1.equals(Object)
is checked.Clearly, the reference of a
is determined before a
is nulled, averting an NPE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With