Page 1 of 1

Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Sat Nov 04, 2017 8:14 am
by saikiran_hegde
Consider the following class...

class Test{
public static void main(String[ ] args){
int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( a [ (a = b)[3] ] );
}
}

What will it print when compiled and run ?

When I ran on my system it prints: 2
So can some one please explain, why in Test it is given as 1.
What I understood is that, array b is assigned to a, so a [ (a = b)[3] ] is accessing { 2, 3, 1, 0 }. :?:

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Sat Nov 04, 2017 11:22 am
by admin
Please make sure you are typing the code exactly as given in the question.

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Mon Jul 09, 2018 9:37 pm
by koremandar967
I didn't understand array indexing after a[(a=b)[3]] this statement. Plz anyone can explain it??

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Tue Jul 16, 2019 10:01 pm
by pats_java
It took me some time to understand. a[(a=b)[3]] means. The b elements act as an index of a. so a[3] then look in the b array b[3] is 0 then the final answer is a[0]=1. This is what I understood. Sorry If I didn't explain it properly.

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Mon Jan 04, 2021 4:44 pm
by promtbvb
@koremandar967
1. a[(a=b)[3]], the value of a is saved;
2. (a=b)[3] is evaluated:
2.1 (a=b) => b
2.2 b[3] => 0
3. a[3] => 1

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Tue Apr 26, 2022 9:05 pm
by sivasd
This is indeed a real brainer.
The provided explanation is clear and understandable.
In the expression a[(a=b)[3]], the expression a is fully evaluated before the expression (a=b)[3]; this means that the original value of a is fetched and remembered while the expression (a=b)[3] is evaluated.
But if you could share any references (JLS) / articles that talk more about this, it would be very helpful.
Thank you!

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Wed Apr 27, 2022 3:20 am
by admin
This is actually straight from the JLS. See this: https://docs.oracle.com/javase/specs/jl ... ls-15.10.4

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Wed Apr 27, 2022 4:28 am
by sivasd
I was looking for resources to read more about this concept but couldn't find the appropriate one.
Thanks for sharing the JLS url.

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Tue Jul 11, 2023 7:49 am
by siobhan
Can someone help me understand that when (a=b)[3] is evaluated, a is pointing to b, so you get 0. Should a not now be pointing to b when a[0] is evaluated, so you would get 2? Why are we looking back at a's original array object to get 1?

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Tue Jul 11, 2023 10:47 am
by admin
>Why are we looking back at a's original array object to get 1?
The explanation already explains it in detail.
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.
In the expression a[(a=b)[3]], the expression a is fully evaluated before the expression (a=b)[3]; this means that the original value of a is fetched and remembered while the expression (a=b)[3] is evaluated. This array referenced by the original value of a is then subscripted by a value that is element 3 of another array (possibly the same array) that was referenced by b and is now also referenced by a. So, it is actually a[0] = 1.
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.
Let me know which part of the explanation you have trouble understanding.

Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036

Posted: Tue Dec 05, 2023 10:02 am
by iulian
Hi,
it's better to keep it simple:

int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
a [ (a = b)[3] ]


a [ (a = b) [ 3 ] ] = a[ b [ 3 ] ]---> b[ 3 ] = 0--->a [ 0 ] = 1;

Sincerely,

Iulian