Please look at the below code snippet and let me know how the out comes out as 1 2 .
int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( a [ (a = b)[3] ] );
System.out.println(a[0]);
Actual answer 1
2
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’ll try to explain:
a [ (a = b)[3] ]will be executed in the following order:a [...]– the arrayawill be read and the reference is stored for that(a = b)– the variableais set to reference arrayb(a=b)[3]– the 4th element of arraybis read (because of step 2) , the value is0a [ (a = b)[3] ]– this is now equal toa[0](because of steps 1 and 3), the value is1a[0]now yields2sinceareferences arrayb(because of step 2) and the first element in that array is2.