Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
Moderator: admin
-
- Posts: 1
- Joined: Sat Nov 04, 2017 8:08 am
- Contact:
Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
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 }.
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 }.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
Please make sure you are typing the code exactly as given in the question.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Mon Jul 09, 2018 9:31 pm
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
I didn't understand array indexing after a[(a=b)[3]] this statement. Plz anyone can explain it??
-
- Posts: 1
- Joined: Tue Jul 16, 2019 9:49 pm
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
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.
-
- Posts: 3
- Joined: Thu Jun 11, 2020 4:17 pm
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
@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
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
-
- Posts: 4
- Joined: Mon Apr 18, 2022 1:40 am
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
This is indeed a real brainer.
The provided explanation is clear and understandable.
Thank you!
The provided explanation is clear and understandable.
But if you could share any references (JLS) / articles that talk more about this, it would be very helpful.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.
Thank you!
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
This is actually straight from the JLS. See this: https://docs.oracle.com/javase/specs/jl ... ls-15.10.4
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Mon Apr 18, 2022 1:40 am
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
I was looking for resources to read more about this concept but couldn't find the appropriate one.
Thanks for sharing the JLS url.
Thanks for sharing the JLS url.
-
- Posts: 1
- Joined: Tue Jul 11, 2023 7:45 am
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
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?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
>Why are we looking back at a's original array object to get 1?
The explanation already explains it in detail.
The explanation already explains it in detail.
Let me know which part of the explanation you have trouble understanding.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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Wed Nov 22, 2023 1:59 am
- Contact:
Re: Creating and Using Arrays enthuware.ocajp.i.v8.2.1036
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
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
Who is online
Users browsing this forum: No registered users and 116 guests