Java Reference
In-Depth Information
}
static Test1 favorite(){
System.out.print("Mount ");
return null;
}
public static void main(String[] args) {
favorite().mountain();
}
}
which prints:
Mount Monadnock
Here favorite() returns null , yet no NullPointerException is thrown.
Example 15.12.4.1-2. Evaluation Order During Method Invocation
As part of an instance method invocation (§ 15.12 ), there is an expression that denotes
the object to be invoked. This expression appears to be fully evaluated before any part
of any argument expression to the method invocation is evaluated.
So, for example, in:
Click here to view code image
class Test2 {
public static void main(String[] args) {
String s = "one";
if (s.startsWith(s = "two"))
System.out.println("oops");
}
}
the occurrence of s before “ .startsWith ” is evaluated first, before the argument expres-
sion s = "two" . Therefore, a reference to the string "one" is remembered as the target
reference before the local variable s is changed to refer to the string "two" . As a result,
the startsWith method is invoked for target object "one" with argument "two" , so the res-
ult of the invocation is false , as the string "one" does not start with "two" . It follows that
the test program does not print “ oops ”.
15.12.4.2. Evaluate Arguments
The process of evaluating the argument list differs, depending on whether the method being
invoked is a fixed arity method or a variable arity method (§ 8.4.1 ).
Search WWH ::




Custom Search