6

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

10
  • What was your intension? Are you sure that you want to assign b to a (a=b), or did you want to compare anything (==)? Commented Mar 22, 2012 at 9:31
  • 2
    Whatever his intention is, this is an interesting result. Commented Mar 22, 2012 at 9:34
  • This is not production code :), Its more learning code ... I am just trying to wrap my head around the order of execution and I agree it look crap :) Commented Mar 22, 2012 at 9:36
  • 2
    @enobayram - This is the result I would expect. He first assignes b to a, then prints the value of index 3 and finally the value of index 0. Commented Mar 22, 2012 at 9:38
  • @Sudarshan just look at the resulting bytecode Commented Mar 22, 2012 at 9:41

3 Answers 3

7

Seriously, what is the purpose of this? Why would you ever wanna do something that makes the code so unreadable. What would you expect the outcome to be?

The result of System.out.println( a [ (a = b)[3] ] ); has to do with the order in which things are pushed to the evaluation stack ... e.g.

  1. reference to a
  2. change reference stored in a to that stored in b
  3. evaluated b[3] => 0
  4. print index 0 of the array to which reference was pushed in 1.), i.e. the original a

so it prints the element at 0 of the original a array

System.out.println(a[0]); is then simply b[0]

Sign up to request clarification or add additional context in comments.

4 Comments

b[3] is 0 (fourth element), that's why 1 is printed, it's a[0]. However, it's really strange that the original a is still evaluated in step 4, don't know if it's part of the specs...
Yup thanks :), look at my above comment to know why i wrote such a question in the first place :)
no it is not strange at all, if you read the bytecode you'll see in which order things are pushed and popped from the stack. Anyways, it just server him right to get the unexpected result when doing stupid things such as a[ (a=b)[3] ]
To me this looks more like an exercise question on a textbook, with the intent of testing readers on a concept. I don't think the person asking the question would ever use it anywhere.
6

I'll try to explain:

a [ (a = b)[3] ] will be executed in the following order:

  1. a [...] - the array a will be read and the reference is stored for that
  2. (a = b) - the variable a is set to reference array b
  3. (a=b)[3] - the 4th element of array b is read (because of step 2) , the value is 0
  4. a [ (a = b)[3] ] - this is now equal to a[0] (because of steps 1 and 3), the value is 1

a[0] now yields 2 since a references array b (because of step 2) and the first element in that array is 2.

Comments

0

First two lines initialize your arrays. First sysout assigns b to a then prints a[3], ie. your a is now having values {2,3,1,0}. Second sysout prints a[0].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.