No, the array won't grow. If you are used to C/C++ and the way that C/C++ treats arrays, I think this is the cause for this confusion.
In C, a 2D array is not a double pointer. Once defined as array[x][y], it will always have x rows and y columns. Besides this, assigning a value to array[k] is simply illegal. This is closely related to the way C treats arrays internally, the way arrays are stored into machine memory.
A Java 2D-array does not behave like a C 2D. It is much more like double pointer or a pointer to an array in C: int * array. This means that array can have X elements and array can have Y elements.
The 2D array in java is just an array of 1D-arrays of any sizes. It's just a special case that initialization like new int[x][y] creates x 1D-arrays, all with y columns. The 1D-array elements can be reassigned and you may very well have array[k] = null !
To go back to the C comparison, think of the argv argument for the main() function, which is defined like char ** or char * argv, meaning that the arguments may very well have different lengthes. This is the closest representation of a Java array.
I hope this answers your question. [ October 24, 2006: Message edited by: Costi Ciudatu ]
In short I'd just say you initially assigned an array of length 1 to a, you then re-assigned a different array (of lenth 4) to a, so ofcourse the System.out.println(a) has no problem.
"a can hold a 1-D array with size 1 . but it holding 1-D array with size 4." - that is incorrect. a can hold ANY 1-d int array of any size. It initially has an array of size 1, it then has a different one of size 4.
Yes, exactly. To speak java now, in int you hold an array of references to int objects. In initialization like arr = new int you make arr = new int; arr = new int. When you do: arr = new int, the former value of arr (which was an int) gets lost (eligible for GC), and arr now holds a reference to the new object that was just created (which is an int). The references stored in int are to int objects of any size not just to a particular int[X]. [ October 24, 2006: Message edited by: Costi Ciudatu ]