The questions is if the above class will print '2' when compiled and run.
I chose true but its false
Below is the explanation given by JQ+ but its not clear to me
Explanation: 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. The embedded assignment of 2 to index occurs before the check for a null pointer. 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.
Could some one kindly clear up/explain it. Waiting for a favorable reply.Thanks in advance. Kind Regards. Hasnain Javed Khan.
The key to understanding this is to know that as assignment expression like "index=2" evaluates to the value being assigned, and that "++" increments the number/variable in front of it by 1. So that statement is equivalent to
posted 12 years ago
Hello All, My confusion is that the evaluation of dimension expression in the array access expression i.e [index=2] is evaluated first before the array reference is resolved or does it depend on the dimension expression inside the square brackets e.g array_ref will resolve the reference variable directly and array_ref[index=2] will resolve the array dimension first and then resolve the array reference variable ?? Hope the question is clear and not confusing. Waiting for a favorable reply. Thanks in advance. Kind Regards. Hasnain Javed Khan.
posted 12 years ago
Actually, the question is a bit hard to understand due to the complete lack of punctuation.
The array index can't be resolved until the assignment is carried out, because the value of the array index is not known before that; does that answer the question?