Why no compiler-warning at invalid array-initializing ?

Discussion in 'Java' started by carlos@gkpwdun.com, Nov 3, 2005.

  1. Guest

    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 ?
    , Nov 3, 2005
    #1
    1. Advertising

  2. 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 ?


    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, Nov 3, 2005
    #2
    1. Advertising

  3. Andrew Thompson wrote:

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


    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..
    Andrew Thompson, Nov 3, 2005
    #3
  4. Guest

    >> 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 ?
    , Nov 3, 2005
    #4
  5. Ross Bamford Guest

    On Thu, 03 Nov 2005 15:46:49 -0000, <> 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 ?

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

    --
    Ross Bamford -
    Ross Bamford, Nov 3, 2005
    #5
  6. 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.
    Thomas G. Marshall, Nov 3, 2005
    #6
  7. 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.


    >
    > Is this just your gues or do you know for a fact that
    > this is the reason ?
    Thomas G. Marshall, Nov 3, 2005
    #7
  8. Roedy Green Guest

    On Thu, 03 Nov 2005 16:18:29 +0100, wrote, quoted
    or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 3, 2005
    #8
  9. HalcyonWild Guest

    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];
    HalcyonWild, Nov 3, 2005
    #9
  10. zero Guest

    "HalcyonWild" <> wrote in
    news::

    >
    > 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.
    zero, Nov 3, 2005
    #10
  11. HalcyonWild Guest

    zero wrote:

    > "HalcyonWild" <> wrote in
    > news::
    >
    > >
    > > 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.
    HalcyonWild, Nov 7, 2005
    #11
  12. zero Guest

    "HalcyonWild" <> wrote in
    news::

    >
    > zero wrote:
    >
    >> "HalcyonWild" <> wrote in
    >> news::
    >>
    >> >
    >> > 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]);
    zero, Nov 7, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    837
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,656
    Smokey Grindel
    Dec 2, 2006
  3. Varun  Kacholia
    Replies:
    4
    Views:
    430
    Frank Schmidt
    Mar 2, 2006
  4. vduber6er
    Replies:
    3
    Views:
    505
    Frederick Gotham
    Oct 24, 2006
  5. Johannes Schaub (litb)

    Why is initializing a FAM invalid?

    Johannes Schaub (litb), Nov 5, 2010, in forum: C Programming
    Replies:
    30
    Views:
    898
    Marcin Grzegorczyk
    Nov 23, 2010
Loading...

Share This Page