I thought it will be a runtime exception. but the answer says Prints null and runtime exception Can anybody explains why is it so? [ December 15, 2006: Message edited by: Sanjeev Kumar Singh ]
I tried this, and I understood it like, you are assigning in Line 7 a3[0] = a3[1] = a3[2] = a2; which is as good as saying, = new Main[2][1]; so it gives the exception. Try changing the line 8 as ystem.out.print(a3[2][2]);, still it gives you the exception. But when you say System.out.print(a3[2][1]);, it prints null. Hope you got the clue.
So in the first case it prints null for a3[2][2][2]
where as after the re assignment
what you have is
So now when you try to access the a3[2][2][2], sure their is an exception Here there is no a3[0-1-2][2] Try to follow the internal representation of the arrays in heap, then its quiet easy.
As per as the thigs which I understands The first SOP should print null and The second SOP should prints ArrayIndexOut..Exception but I gets the reverse order.Is this happening with you guys also?
Sanjeev, it is not the reverse order. Comment the last line and still you will get null for // 4. What we have to know is that whenever there is a run time exception, it gets printed out first followed by any println statements in the order they are written.
Jothi, I think you misunderstood a bit. After runtime exception only the cause of the exception is printed and the program terminates if there is no handler.
Keep it simple man. Just after the exception there is nothing going to get printed except the cause of the exception(trace); In our discussion when first print statement is executed, there is null stored at the index a3[2][2][2] and is printed But after the assignment, we don't have a a3[2][2][2] So in the second print statement its an array index out of bounds exception. Then the program halts and none of the statement is executed until unless there is try{}catch{} module
Hope you understood. Just try a few exception creating statements and look at the flow. You will make it easy with practise.
Thiyanesh, thanks for the explanation. Can you also please explain once again the assignment for the arrays. I'm only partially understanding your explanation??
Jothi, Let me know if i am going outside, Think of pointers in "C"
So for a3[][][], We have each element of a3[3rd Dim] holds a reference to another two dimensional array. Further each of a3[2nd dim] holds a reference to another one dimensional array. Then each of these final one dimensional arrays have either primitives for if its a primitive array or references for the objects if an object array.
The point is the real values of primitives or the references to our objects are placed as the one dimensional array.
a2[][] is a two dimensional array. a1[] is a single dimensional array. Each 2nd dimension in a two dimensional array should have a reference to either a single dimensional array or null;
Hence the assignment a2[0] = a2[1] = a1, assigns the single dimensional array a1 to the 2nd dimensions(from right) of a2.
It will much helpful for you to understand using primitive arrays.
If its an array of objects, then even a1[0] will have the address of the object and another level of indirection.