array
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi all,
Consider this code,
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 ] ] ); // o/p is 1
}
}
Can anyone explain the output
Thx
Aruna
Consider this code,
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 ] ] ); // o/p is 1
}
}
Can anyone explain the output
Thx
Aruna
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Think about the order of evaluation of the complex mess that is the index for a
( a = b ) result b
b[ 3 ] result 0
a[ 0 ] result 1
Bill
( a = b ) result b
b[ 3 ] result 0
a[ 0 ] result 1
Bill
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
OK
a [ ( a = b ) [ 3 ] ] is a [x] , where
x = ( (a = b) [ 3 ] )
Let's print x:
System.out.println( (a = b) [ 3 ] ); // output is 0
Actually (a = b) [ 3 ] means a[3] except var a now points to array b, so whole expression means b[3], which is 0
And finally, a[x] = a[0] = 1, and I believe var a here still refers to array a, not b!
I wonder if somebody can translate all above to English�
a [ ( a = b ) [ 3 ] ] is a [x] , where
x = ( (a = b) [ 3 ] )
Let's print x:
System.out.println( (a = b) [ 3 ] ); // output is 0
Actually (a = b) [ 3 ] means a[3] except var a now points to array b, so whole expression means b[3], which is 0
And finally, a[x] = a[0] = 1, and I believe var a here still refers to array a, not b!
I wonder if somebody can translate all above to English�
Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi all,
An old thread was found when I do a search on "array".
I'm a bit confused here.. so, could anyone please help me figure this one out?
As Mapraputa Is described, a[x] is still pointing to the old reference of array a. Could anyone explain why??
I have tried the following code:
The current code will give an output of 1, because we still refer to the old a. But, if I run the "commented-out" code, then I'll get 2 as the output. Could anyone explain why?
And, if I still use the current code, then would it be in "lineOfGC", the a object is not eligible for GC??
Any help is highly appreciated..
Thanks in advance.
- eric
ps: Another thing to note is that when I try to print the elements of a, after the "lineOfGC", I get the elements of b.
An old thread was found when I do a search on "array".
I'm a bit confused here.. so, could anyone please help me figure this one out?
As Mapraputa Is described, a[x] is still pointing to the old reference of array a. Could anyone explain why??
I have tried the following code:
The current code will give an output of 1, because we still refer to the old a. But, if I run the "commented-out" code, then I'll get 2 as the output. Could anyone explain why?
And, if I still use the current code, then would it be in "lineOfGC", the a object is not eligible for GC??
Any help is highly appreciated..
Thanks in advance.
- eric
ps: Another thing to note is that when I try to print the elements of a, after the "lineOfGC", I get the elements of b.
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
This is the explanation that I could come up with:
"System.out.println( a [ ( a = b ) [ 3 ] ] ); "
results in a[b[0]] ... b/c evaluation is done left to right...
now in a[(a=b)[3]], the first ex. a does not need any further evaluation... next expression a=b results in a pointing to b's elements; 3 does not need any further eval... thus (a=b)[3] results in b[3] which is 0....adding it all it is a[0] which results in 1
Vaneet
"System.out.println( a [ ( a = b ) [ 3 ] ] ); "
results in a[b[0]] ... b/c evaluation is done left to right...
now in a[(a=b)[3]], the first ex. a does not need any further evaluation... next expression a=b results in a pointing to b's elements; 3 does not need any further eval... thus (a=b)[3] results in b[3] which is 0....adding it all it is a[0] which results in 1
Vaneet
Originally posted by Aru:
Hi all,
Consider this code,
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 ] ] ); // o/p is 1
}
}
Can anyone explain the output
Thx
Aruna
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Aru,
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi All,
Just to clear Bill's short explanation (which is right on!). Eric's question is a valid one. The problem that everyone seems to be missing is that the assignment operator is the last one to complete in the line. That is why the print will still be referring to the original a array because the assignment happens afterwards (lowest eval priority). Also, Eric correctly points out that after the print line is complete variable a is pointing the same array as variable b.
Bill's first line says (a = b) result = b, what is not said is that the assignment has not happened yet, we are just using the result to complete the print! The assignment will happen after the value a[0] has been used for the print.
Regards,
Manfred.
Just to clear Bill's short explanation (which is right on!). Eric's question is a valid one. The problem that everyone seems to be missing is that the assignment operator is the last one to complete in the line. That is why the print will still be referring to the original a array because the assignment happens afterwards (lowest eval priority). Also, Eric correctly points out that after the print line is complete variable a is pointing the same array as variable b.
Bill's first line says (a = b) result = b, what is not said is that the assignment has not happened yet, we are just using the result to complete the print! The assignment will happen after the value a[0] has been used for the print.
Regards,
Manfred.
Eric Pramono
Ranch Hand
Posts: 74
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks a lot, Manfred.
That really helps.
- eric
That really helps.

- eric
| If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |







