class ArrayTest{
public static void main(String arg[]) {
int a[]=new int[0];
a[0]=5;
}
}
It will compile fine,when I run this code,its saying ArrayIndexOutOfBoundException....
As we know array declaration should have one integer value,even if I give -1 also it will compile. But in runtime it is saying NegativeIndex or ArrayIndexOutOfBound for -1 or 0 respectivley.
Why it is doing like this? While doing compiling itself why don't the compiler has do that? I accept for the variable initialization since it has clue for the variable in the compilation time,but for straight forward initialization like above should caught by the compiler... is my understanding is wrong?
Tuesday, April 10, 2007
Subscribe to:
Post Comments (Atom)
2 comments:
Valid point! But I see it as a feature request to Sun JDK Compiler Spec. I would draw an analaogy to this to generics in jdk 1.5 as it was a much needed datatype validation from the compiler and it didn't existed till 1.4.
Raise a JSR for this :)
Thanks,
Senthil Balakrishnan.
Good Blog saro..
Just read JVM Spec and got to know that "All array accesses are checked at run time"
Link here:http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#25959
Post a Comment