10 Replies Latest reply: Jun 29, 2012 8:38 AM by BIJ001 RSS

    java array doubt..

    946308
      public static void main(String Args[]){
           int[] array = new int[3];
           array[5] = 2; // why does this passes compilation as compiler knows that array[] is defined to hold only 3 integers !
           
           System.out.println(array);
           }
        • 1. Re: java array doubt..
          NexusChris
          This is runtime exception would not throw during compilation, as you run it will die.
          • 2. Re: java array doubt..
            Kayaman
            943305 wrote:
            why does this passes compilation as compiler knows that array[] is defined to hold only 3 integers !
            Because in most cases the compiler does not know the array's size. Implementing this would have very little use in real programming.
            • 3. Re: java array doubt..
              946308
              Yes. it is runtime exception but i want to know WHY doesn't this get checked at compile time ?
              • 4. Re: java array doubt..
                EJP
                Because it can't. You declared the variable as an in[], so it's an int[]. You initialized it as int[3] but the compiler doesn't retain that information as part of the declaration. In a more complex program it would be wrong to do so because the variable could have been reassigned.
                • 5. Re: java array doubt..
                  gimbal2
                  943305 wrote:
                  Yes. it is runtime exception but i want to know WHY doesn't this get checked at compile time ?
                  Because it isn't implemented. No need to think of wild stories around it or find reasons what would make it impossible - it isn't checked because the check isn't implemented.

                  Asking WHY questions is so incredibly useless. Who cares? Its like that, better pay attention to your work in stead of expecting the tools to do the work for you.

                  Edited by: gimbal2 on Jun 28, 2012 1:30 AM
                  • 6. Re: java array doubt..
                    946368
                    Hi,

                    You know there are 2 types of exceptions java handles.
                    1. Compile Time Exception ( Checked exception ).
                    2. Run Time Exception (Unchecked Exception ).

                    What code u have posted above throws java.lang.ArrayIndexOutOfBoundsException exception and this exception falls under run time exception 2nd in above list.

                    So that it was not checked at compile time.

                    Regards,
                    Sarfaraz
                    • 7. Re: java array doubt..
                      EJP
                      You know there are 2 types of exceptions java handles.
                      1. Compile Time Exception ( Checked exception ).
                      Wrong. All exceptions occur at runtime.
                      2. Run Time Exception (Unchecked Exception ).
                      Wrong again.
                      What code u have posted above throws java.lang.ArrayIndexOutOfBoundsException exception and this exception falls under run time exception 2nd in above list.
                      Correct.
                      So that it was not checked at compile time.
                      Correct.

                      Unfortunately that doesn't answer his question. He is asking why.
                      • 8. Re: java array doubt..
                        BIJ001
                        As EJP pointed out, the type of the variable is int[].

                        As per language definition, the type of an array depends only on the element type and not on the dimension. Every array of a given element type shares the same array type regardless of the very dimensions. So the type checking can not cover this.
                        • 9. Re: java array doubt..
                          gimbal2
                          But the OP isn't really asking about type checking, he's asking about compile time range checks.
                          • 10. Re: java array doubt..
                            BIJ001
                            But what happens is compile time type checking, which, as known and explained, does not cover index range checking.