In the code below,I have defined a (2x2) 2D array, arr
. And then I declare a one dimensional String array a
with 5 elements, and assign it to the first row of the 2D array. Why am I allowed to do this, and why does not it throw array index out of bound exception?
I think it may be due to the reference assignment but still I am confuse about how one is allowed to change the array dimension at runtime.
String a[] = { "1", "2", "3", "4", "5" };
String[][] arr= new String[2][2];
int x;
arr[0] = a;
x = arr[0].length;
System.out.print("The row length is "+ x);
Output:The row length is 5.