File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "array" Watch "array" New topic
Author

array

Aru
Ranch Hand

Joined: Jul 20, 2000
Posts: 112
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12267
    
    1
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

Java Resources at www.wbrogden.com
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
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�

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
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.
Vaneet Bhutani
Greenhorn

Joined: Sep 13, 2000
Posts: 18
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
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

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Thanks a lot, Manfred.
That really helps.
- eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array
 
Similar Threads
Array
Operators
dan exam doubt 8
help
Got Confused With OutPut... Can Any One Explain Me..