Originally posted by M Krishnan:
Is this allowed?
int[][] a = new int[5][-1];
How come the compiler allows it but ends up in runtime exception - NegativeArraySizeException?
what exactly are the things the compiler checks in a declared array?
Object creating is the matter of runtime. What compiler can check is for the
proper syntax allowed in
Java as:
int [3] arr; //Compiler error, because virtual machine does not allocate
space until you actually instantiate the array object.
int[] arr = new int[]; //compiler error, missing dimension
int[] [] arr = new int[][3]; //compiler error too
int[] = new int[3]{12,3,4};//compiler error
So these are some cases where compiler can check us, but providing negative
array size or size beyond the array scope can only be determined at run
time. And there are exceptions to be thrown if any of such cases occur.
Thanks,