While declaring an array in java we have to dynamically allocate the memory using new keyword.
class array
{
public static void main(String ars[]) {
int A[] = new int[10];
System.out.println(A.length);
}
}
Above code will create a 1D array containing 10 elements, 4 byte each.
and the output will be 10
.
But when you run same code as following:
class array {
public static void main(String ars[]) {
int A[] = new int[0];
System.out.println(A.length);
}
}
Output is 0. I want to know that when you write new int[0]
then do Java allocate some memory for the array or not? If yes how much?