• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Array

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


Thanks in advance,
[ September 13, 2005: Message edited by: Barry Gaunt ]
 
Sanjay Samant
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry in Q2. array a[] is declared as follows
int a[]=null;


Thnaks
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
Exception in thread "main" java.lang.NullPointerException
at arr.main(arr.java:8)
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic