Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.185 :

Posted: Mon Aug 14, 2017 10:08 am
by Aditya553
I'm unable to understand.Please explain in a simpler manner.

Question -
The following class will print index = 2 when compiled and run.


class Test
{
public static int[ ] getArray() { return null; }
public static void main(String[] args)
{
int index = 1;
try
{
getArray()[index=2]++;
}
catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}

Options -
1)True
2) False

Explanation was given -
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.

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Posted: Mon Aug 14, 2017 9:20 pm
by admin
See my post in this thread https://coderanch.com/t/683214/certific ... ng#3205868
Then apply the same concept here.

HTH,
Paul.

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Posted: Mon Aug 14, 2017 9:31 pm
by Aditya553
So
getArray()[index=2]++;
here first index becomes 2 and than it increases to 3 and than it becomes
null[3]

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Posted: Mon Aug 14, 2017 9:36 pm
by admin
Why do you think index increments to 3?? ++ is not there on index.

The element at index 2 in the array returned by getArray() is incremented.

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Posted: Mon Aug 14, 2017 9:41 pm
by Aditya553
public static int[ ] getArray() { return null; }
so this line means int[] have null values .

getArray()[index=2]++;
this line means index becomes 2 and than in getArray int[] at index 2 (which is null)is incremented.
null cannot be incremented

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Posted: Mon Aug 14, 2017 10:24 pm
by admin
Aditya553 wrote:public static int[ ] getArray() { return null; }
so this line means int[] have null values .
No, it means there is no array at all because the return value is null. There is a difference between a null reference and an array containing nulls. See this: https://stackoverflow.com/questions/274 ... mpty-array
Aditya553 wrote: getArray()[index=2]++;
this line means index becomes 2 and than in getArray int[] at index 2 (which is null)is incremented.
null cannot be incremented
Correct. Since getArray() returns null, you can't even access null[2] let alone increment it. So there will be a NullPointerException, which will be caught by the catch block.

Re: About Question com.enthuware.ets.scjp.v6.2.185 :

Posted: Mon Aug 14, 2017 10:28 pm
by Aditya553
ok got it.
Thanks