Monday, February 8, 2016

Java Array Bounds Sample

 1 // ArrayBounds.java
 2 package  com.jdojo.array;
 3 
 4 public class ArrayBounds {
 5     public static void main(String[] args) {
 6         int[] test = new int[3];
 7             
 8         System.out.println("Assigning 12 to the first element");
 9         test[0] = 12;  // index 0 is between 0 and 2. Ok
10 
11         System.out.println("Assigning 79 to the fourth element");
12         
13         // index 3 is not between 0 and 2. At runtime, an exception is thrown.
14         test[3] = 79; 
15         
16         System.out.println("We will not get here");        
17     }
18 }