I was wondering, why doesn't the java-compiler warn
for incorrect initializing of arrays, for example :
int[] myArray = new int[2];
myArray[3] = 1; // Runtime-error : ArrayIndexOutOfBoundsException.
Isn't it very easy for the compiler to detect this
kind of invalid code ?
Andrew Thompson - 03 Nov 2005 16:33 GMT
> I was wondering, why doesn't the java-compiler warn
> for incorrect initializing of arrays, for example :
[quoted text clipped - 4 lines]
> Isn't it very easy for the compiler to detect this
> kind of invalid code ?
Not if a separate thread is monitoring the array for null,
and, when not null - filling the values to the assigned
length.
[ No, actually, I cannot think of a situation where you
*would* code it that way, but that is not the point. ]
Andrew Thompson - 03 Nov 2005 16:37 GMT
>> I was wondering, why doesn't the java-compiler warn
>> for incorrect initializing of arrays, for example :
>>
>> int[] myArray = new int[2];
>> myArray[3] = 1; // Runtime-error : ArrayIndexOutOfBoundsException.
For some reason I read that as a NullPointerException and
did not look closely at the start of the line..
But, same difference, given the possibility of the..
> ..separate thread..
carlos@gkpwdun.com - 03 Nov 2005 16:46 GMT
>> I was wondering, why doesn't the java-compiler warn
>> for incorrect initializing of arrays, for example :
[quoted text clipped - 8 lines]
>and, when not null - filling the values to the assigned
>length.
?? You mean, the compiler cannot easily detect this kind
of invalid code because it uses an separate thread to
monitor the array for null and eventualy prefilling the
array ?
Is this just your gues or do you know for a fact that
this is the reason ?
Ross Bamford - 03 Nov 2005 17:17 GMT
>>> I was wondering, why doesn't the java-compiler warn
>>> for incorrect initializing of arrays, for example :
[quoted text clipped - 16 lines]
> Is this just your gues or do you know for a fact that
> this is the reason ?
I don't think that's what Andrew meant. Rather, he's saying that this
isn't a compile-time error because the int[] doesn't have a size, and it's
conceivable that your new int[2] could get 'externally changed' at
runtime, before the access to myArray[3] is run. I'm not sure how at risk
your example would be (since another thread doesn't have any reference to
it), and yes it would be pretty easy for the compiler to pick up, but it
doesn't take that many more statements to totally remove that.
I could see logic in your argument if we could do something like:
int[3] threeInts = new int[3];
error = threeInts[4];
but we can't (and don't need to) - an int[] variable holds 'an array of
ints', so the compiler cannot reliably (and so won't ever) infer the size
of an array in the way you suggest. Indeed, there's no way to know whether
a given array can return a given index at runtime until you ask the array
itself, since all arrays of a given element type and the same number of
dimensions are implemented by the same class.

Signature
Ross Bamford - rosco@roscopeco.remove.co.uk
Thomas G. Marshall - 03 Nov 2005 17:39 GMT
carlos@gkpwdun.com coughed up:
>>> I was wondering, why doesn't the java-compiler warn
>>> for incorrect initializing of arrays, for example :
[quoted text clipped - 13 lines]
> monitor the array for null and eventualy prefilling the
> array ?
No, that's not what Andrew was getting at. Please see my post downthread.
> Is this just your gues or do you know for a fact that
> this is the reason ?
Thomas G. Marshall - 03 Nov 2005 17:35 GMT
carlos@gkpwdun.com coughed up:
> I was wondering, why doesn't the java-compiler warn
> for incorrect initializing of arrays, for example :
[quoted text clipped - 4 lines]
> Isn't it very easy for the compiler to detect this
> kind of invalid code ?
Ok, there are a couple concepts going on here.
First of all, in a situation like you just showed, it is possible that the
compiler might be able to guess at the impending error. Especially if the
two statements were in very tight proximity.
You must always remember that except for local variables which live on the
stack, variables that are public or otherwise accessible to threads might
change at any moment. So the myArray you show above might be changed to
myArray = new int[1000];
at some point in time before the assignment is attempted.
Further, you also have to remember also that these exceptions that can be
caught at run time (like you showed as well) and as such may very well be
useful to the application. In a testing environment specifically, it might
very well be the case that for a test harness someone has specifically made
"myArray" too small, just to make sure their code handles such things as
gracefully as possible.
Roedy Green - 03 Nov 2005 20:22 GMT
>int[] myArray = new int[2];
>myArray[3] = 1; // Runtime-error : ArrayIndexOutOfBoundsException.
>
>Isn't it very easy for the compiler to detect this
>kind of invalid code ?
Even if it could, the way it would tell you about it consistently is
with a run time exception.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
HalcyonWild - 03 Nov 2005 21:05 GMT
> I was wondering, why doesn't the java-compiler warn
> for incorrect initializing of arrays, for example :
[quoted text clipped - 4 lines]
> Isn't it very easy for the compiler to detect this
> kind of invalid code ?
Ok, how does the compiler detect this.
int size = Integer.parseInt(System.readLine()); //user enters 20
int [] myArray = new int[size];
// .. some code
sum = sum + myArray[21];
zero - 03 Nov 2005 23:20 GMT
>> I was wondering, why doesn't the java-compiler warn
>> for incorrect initializing of arrays, for example :
[quoted text clipped - 13 lines]
>
> sum = sum + myArray[21];
I don't know about yours, but my compiler is psychic.
HalcyonWild - 07 Nov 2005 12:42 GMT
> >> I was wondering, why doesn't the java-compiler warn
> >> for incorrect initializing of arrays, for example :
[quoted text clipped - 15 lines]
>
> I don't know about yours, but my compiler is psychic.
Well, my point was that if the size of the array is unknown until
runtime, the compiler cannot do anything about it. It is a runtime
exception, not a compile error.
For example( very trivial one), you have code to read a string from the
user and copy individual chars to an array manually. (without using
toCharArray() of String).
There can be many such runtime instances where the size of the array is
unknown until runtime.
zero - 07 Nov 2005 20:34 GMT
>> >> I was wondering, why doesn't the java-compiler warn
>> >> for incorrect initializing of arrays, for example :
[quoted text clipped - 24 lines]
> There can be many such runtime instances where the size of the array is
> unknown until runtime.
Or the index is unknown.
int[] iArray = new int[5];
int index = scanner.nextInt();
System.out.println("iArray["+index+"] = " + iArray[index]);