Win a copy of Design for the Mind this week in the Design forum!

# Question on Array

Sanjay Samant
Greenhorn
Posts: 2
Hi everyone,

I am preparing for SCJP 1.4 exam which is due in October. I came across following confusing questions on arrays .

Q.1
int[] a={1,2,3,4};
int[] b={3,4,5,0};
System.out.println(a[(a=b)[0]]);
Above code compiles okay and prints 1. Explanation is given in mock exam is that araays are always evaluated from left to right i.e. a[b[3]]=> a[0]=>1

Q2.
int a[]=null; // was {1,2,3,4} (edited as next post)
System.out.println(a[m1()]);

Signature of method m1() is as follows
int m1() throws Exception
{ throw new Exception("Error in m1()");}

This compiles okay and throws Exception with message "Error in m1()". If principle of Q.1 is applied to this , this should have thrown NullPointerException. Why it is throwing Exception.

Can anyone put light on this.

[ September 13, 2005: Message edited by: Barry Gaunt ]

Sanjay Samant
Greenhorn
Posts: 2
Sorry in Q2. array a[] is declared as follows
int a[]=null;

Thnaks

Ritu Kapoor
Ranch Hand
Posts: 102
Hi Sanjay,

As far as your first question is concerned, the explanation for that is:
------------------------------------------------------------------------

Q.1
int[] a={1,2,3,4}; //1
int[] b={3,4,5,0}; //2
System.out.println(a[(a=b)[0]]); //3

In line 3, where a=b, a starts refering to the b array or you can say both a and b are now refering to the same array. The value of a becomes
a={3,4,5,0}, now line //3 becomes

a[a[0]] i.e a[3]
which is further equals to 4.

Your Output should be 4 and not 1

------------------------------------------------------------------------
Q 2.
int a[]=null;
System.out.println(a[m1()]); //1

int m1() throws Exception
{
throw new Exception("Error in m1()");
}

I'm bit doubt full about the explanation that I'm going to give you but I'm wrong pls guys do let me know.

As per my knowledge I think at line //1, NullPointer exception is expected because we're trying to access index of an array that is pointing to nothing. But before throwing runtime exception JVM comes across m1() at line //1 which it runs and executes the code in it resulting in exception i.e "Error in m1()" due to which program terminates before throwing NULLPOINTER EXCEPTION.

Akhilesh Trivedi
Ranch Hand
Posts: 1599
Samant!

public class arr
{
public static void main(String arga[])
{
int[] a=null;
int[] b={3,4,5,0};
System.out.println(a[(a=b)[0]]);
}
}

Says.....
at arr.main(arr.java:8)

srikanth reddy
Ranch Hand
Posts: 252
ritu...

In line 3, where a=b, a starts refering to the b array or you can say both a and b are now refering to the same array. The value of a becomes
a={3,4,5,0}, now line //3 becomes
a[a[0]] i.e a[3]
which is further equals to 4.

in this i have a doubt when you are saying that a={3,4,5,0} then when you are using a[3] why not the o/p is 0 instead of 4 as a={3,4,5,0}..
plz throw some light on it ..

srikanth

Kicky San
Greenhorn
Posts: 18
within the print statement it is taking the older value of the a array.so it throws null pointer exception in the previous post from akhil.

take this example..
class arr
{
public static void main(String[] args)
{
int[] a={1,2,3,4};
int[] b={3,4,5,0};
//System.out.println(a=b);
//a = b; // 8
System.out.println(a[(a=b)[0]]);
System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]);
}
}

the output is
4
3 4 5 0

it is looking into the olden a[] with values {1,2,3,4}

if you uncomment line //8 , you will get the value 0 (a will have the new values now)

can anyone explain why this happens?
[ September 13, 2005: Message edited by: karthick sundararajan ]

Shivani Chandna
Ranch Hand
Posts: 380
The answers to all your questions - lies in JLS..let me quote some stuff from it over here...

15.13.2 Examples: Array Access Evaluation Order (Java Language Specification)

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. For example, in the (admittedly monstrous) 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.
Thus, the example:

prints:

14

because the monstrous expression's value is equivalent to a[b[3]] or a[3] or 14.
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. Thus, the example:

prints:

java.lang.Exception: Ciao, index=1

because the embedded assignment of 2 to index never occurs.

--
Shivani.