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.

arr[0]is a reference to a 1D array, and you may make any assignment you wish. In Java, 2D (or 3D, etc.) array can be jagged, meaning that the second dimensions do not all have to be the same.String[][] arr = new String[1][];is already valid, whereas it throws an error if you either remove the the size for first dimension or just initialize the size of the second dimension