In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
class X {
public static int [] apply(){
if (Y.i==Y.i)throw new NullPointerException();
return new int[]{10,8,4,6};
}
}
public class Y {
public static int i = 0;
public static void main(String[] args) {
try {
System.out.println(X.apply()[i = 3]);
} finally {
System.out.println(i);
}
}
}
and getArray()[index=2] actually is a element of the array. So by the explanation " In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.", the getArray () should be evaluated first, then the index=2 statement. In this case, getArray() throws a NullPointerException which leaves the index=2 statement unevaluated. So, what's wrong here?
Last edited by admin on Sun Feb 26, 2017 10:19 pm, edited 1 time in total.
Reason:Please enter code inside [code] [/code] tags
In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
class X {
public static int [] apply(){
if (Y.i==Y.i)throw new NullPointerException();
return new int[]{10,8,4,6};
}
}
public class Y {
public static int i = 0;
public static void main(String[] args) {
try {
System.out.println(X.apply()[i = 3]);
} finally {
System.out.println(i);
}
}
}
Yes, i will remain 0. i = 3 will not be executed.
If you like our products and services, please help us by posting your review here.
Your getArray method doesn't throw a NullPointerException. It returns null. So the statement, "if evaluation of the expression to the left of the brackets completes abruptly," doesn't apply here.
If you like our products and services, please help us by posting your review here.
Can someone please give an example when it applies this rule "if evaluation of the expression to the left of the brackets completes abruptly"? Meaning in which cases the evaluation completes abruptly?
Guys, I think I understand... It's very easy in fact!
Look at two examples:
1) public static int[ ] getArray() { return null; } // [index=2] will work!!! This is case form question.
2) public static int[ ] getArray() { throw new RuntimeException(); } //[index=2] will not work. RTE - it is abnormal finish!
For 1:
If the array reference expression produces null(!!!!!!) instead of a reference to an array, then a NullPointerException is thrown at runtime, but only(!!!) after all parts of the array reference expression have been evaluated and only if these evaluations completed normally.
return null - it is normal finish!
For 2:
Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
RunTimeException - it is abnormal finish!