previous | start | next

Array index Checking

If an index is out of range, this is checked at run time.

    1   
    2   public class ex2
    3   {
    4     
    5     public static void main(String[] args)
    6     {
    7       String str[] = new String[5];
    8       str[0] = "one";
    9       str[1] = "two";
   10       str[2] = "three";
   11       str[3] = "four";
   12       str[4] = "five";
   13   
   14       for(int i = 0; i <= str.length; i++) {
   15         System.out.println(str[i]);
   16       }
   17   
   18     }
   19   
   20   }

Output:
one
two
three
four
five
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
      at ex2.main(ex2.java:15)



previous | start | next