Why no compiler-warning at invalid array-initializing ?

C

carlos

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 ?
 
A

Andrew Thompson

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 ?

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. ]
 
A

Andrew Thompson

Andrew said:
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..
 
C

carlos

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 ?

Not if a separate thread is monitoring the array for null,
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 ?
 
R

Ross Bamford

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 ?

Not if a separate thread is monitoring the array for null,
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 ?

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.
 
T

Thomas G. Marshall

(e-mail address removed) coughed up:
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 ?

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.
 
T

Thomas G. Marshall

(e-mail address removed) coughed up:
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 ?

Not if a separate thread is monitoring the array for null,
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 ?

No, that's not what Andrew was getting at. Please see my post downthread.
 
R

Roedy Green

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.
 
H

HalcyonWild

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 ?


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];
 
Z

zero

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 ?


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];

I don't know about yours, but my compiler is psychic.
 
H

HalcyonWild

zero said:
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 ?


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];

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.
 
Z

zero

(e-mail address removed) wrote:

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 ?


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];

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.

Or the index is unknown.

int[] iArray = new int[5];
int index = scanner.nextInt();
System.out.println("iArray["+index+"] = " + iArray[index]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top