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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
saikiran_hegde
Posts: 1
Joined: Sat Nov 04, 2017 8:08 am
Contact:

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

Post 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 }. :?:

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

koremandar967
Posts: 1
Joined: Mon Jul 09, 2018 9:31 pm
Contact:

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

Post by koremandar967 »

I didn't understand array indexing after a[(a=b)[3]] this statement. Plz anyone can explain it??

pats_java
Posts: 1
Joined: Tue Jul 16, 2019 9:49 pm
Contact:

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

Post 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.

promtbvb
Posts: 3
Joined: Thu Jun 11, 2020 4:17 pm
Contact:

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

Post 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

sivasd
Posts: 4
Joined: Mon Apr 18, 2022 1:40 am
Contact:

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

Post 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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

sivasd
Posts: 4
Joined: Mon Apr 18, 2022 1:40 am
Contact:

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

Post 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.

siobhan
Posts: 1
Joined: Tue Jul 11, 2023 7:45 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

iulian
Posts: 4
Joined: Wed Nov 22, 2023 1:59 am
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 82 guests