programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Array expression evaluation

Ranch Hand
Posts: 424
• 1
• Number of slices to send:
Optional 'thank-you' note:
Source: Enthuware v4

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.

I m not getting this concept.

Ranch Hand
Posts: 1274
• 2
• Number of slices to send:
Optional 'thank-you' note:
Howdy!
What is
a [ (a = b)[3] ] ?

It is the (a = b)[3] -th element of array a.

With a = b, the variable a now points to the array object of the second line (where also b points to).
so (a = b)[3]
points to the 3 rd element of the b array. The zero.

The total expression
a [ (a = b)[3] ]
is the same as
a [0]

But didn't we reassign a to b in the inner part?
Yes, but:

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.

.
The chosing of array a has been done before the reassignment inside the brackets. Therefore the one and not the two is printed.

By the way, after that line the reassignment will be valid for following code.

prints
1
1
2

Yours,
Bu.

Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:

I agree with the above explanation , but when i first look at the code , i took it this way
(a = b)[3] ==> a[3]=b[3] ==> a[3]=0 ==> 0
so a [ (a = b)[3] ] ==> a[0]

Burkhard Hassel
Ranch Hand
Posts: 1274
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Gautam Pandey:

I agree with the above explanation , but when i first look at the code , i took it this way
(a = b)[3] ==> a[3]=b[3] ==> a[3]=0 ==> 0
so a [ (a = b)[3] ] ==> a[0]

(a = b)[3] ==> a[3]=b[3]
No, here is the mistake. You don't reassign individual elements of the arrays with (a=b)[3]

Yours,
Bu.

Gautam Pandey
Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:
Got it !
The (a=b) is evaluated first then the index [3] is applied to the new reference array (now a is pointing to b) so now it's a[3] which is actually b[3]

Ranch Hand
Posts: 40
• Number of slices to send:
Optional 'thank-you' note:
Thanks for the wonderful explanation

Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
... But element 3 in b[2,3,1,0] is 0. Why the out put is 1?

author
Posts: 23928
142
• Number of slices to send:
Optional 'thank-you' note:

Mian Amjad wrote:... But element 3 in b[2,3,1,0] is 0. Why the out put is 1?

Element three is zero... okay. You got that... Then you are using zero to dereference the "a" array -- and the zeroth element is one.

There is also another point... the side effect of changing the "a" array reference to point to the same array as the "b" array reference doesn't happen until after the "a" reference is dereferenced, so, it is using the previous array (before the side effect).

Henry

Mian Amjad
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Thanks Henry, so the println uses index [3] of b arry (which is 0) to retrieve value from array a (a[0]), which is 1. Got it.

Ranch Hand
Posts: 128
• Number of slices to send:
Optional 'thank-you' note:
Can you clearify this step:
(a = b)[3] ==> a[3]=b[3]

Marshal
Posts: 76884
366
• Number of slices to send:
Optional 'thank-you' note:

John Losty wrote:. . . (a = b)[3] ==> a[3]=b[3]

That is different from what was originally written. Burkhard Hassel explains that the ==> token, which isn't a valid Java® operator, was simply used to show the current values of the different variables.
I hope that sort of code doesn't come up in the exam because it is horrible style and confusing, and if used at work will ensure that you are fired pretty quickly.

Evaluating that code requires two bits of information:-
• 1: Evaluation order is left to right as described in the Java® Language Specification (=JLS)
• 2: As described in the same JLS section, precedences of operators and () are preserved.
• So, ignoring the System.out.println part and only looking at the evaluation of the expression passed as its argument:-It would be easier to understand like this:-You can then see that the result will be 11 and there is no risk of confusing it with the 3rd element of the other array.

As stated previously the assignment in that horrible bit of code does not equate to a[3] = b[3], not even if the “after” value of a[3] is the same as the “before” value of b[3]. That is why the output of my last line (5) changes from 11 to 2.

John Losty
Ranch Hand
Posts: 128
• Number of slices to send:
Optional 'thank-you' note:
aha, the way I see it is this
a=b -> the value of this expresion is b
b[3] -> the value of this is 0
a[0] -> the velue of this is 11

Campbell Ritchie
Marshal
Posts: 76884
366
• Number of slices to send:
Optional 'thank-you' note:
Yes, but that is only part of the evaluation procedure.

 Your mind is under my control .... your will is now mine .... read this tiny ad the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising
reply
Similar Threads