/** * A Simple Example that only declares an array without creating an instance. */ public class DeclareJavaArrayNoInstance { public static void main(String[] args) { int[] myArray; } }This variable is unusable in our case because it could not contain data. We need to initialize it before we could perform operations on its items.
/** * A Simple Example that Creates an Array using the new operator */ public class SimpleCreateArrayExample { public static void main(String[] args) { int[] myTestArray = new int[4]; } }The code "new int[4]" creates an instance of array with 4 items. Since we are declaring an array of int, each item in the array contains the default value for an int which is 0. Hence, if we modify the code to be:
/** * A Simple Example that Creates an Array using the new operator and prints the values of each item */ public class SimpleCreateArrayExample { public static void main(String[] args) { int[] myTestArray = new int[4]; System.out.println(myTestArray[0]); System.out.println(myTestArray[1]); System.out.println(myTestArray[2]); System.out.println(myTestArray[3]); } }The output will be:
0 0 0 0As we see from the code, arrays in Java are 0-based. It means the index of the first item starts with 0. So the valid list of index for an array with 4 elements are: 0, 1, 2, and 3 and not 1, 2, 3, and 4. If we access values below 0 or beyond the capacity of the array, we will get an exception. For example:
/** * A Simple Example that tries to access an item beyond capacity of an array */ public class OutOfBoundsExample { public static void main(String[] args) { int[] myTestArray = new int[4]; System.out.println(myTestArray[4]); } }
Will throw an exception
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at OutOfBoundsExample.main(OutOfBoundsExample.java:7)Because 4 is the index of the 5th element in array, but we only created an array of 4 elements.
Another caveat we need to check is when we declare an array of objects. Below is an example to ullustrate the point
/** * A Simple Example that Creates an Array of Objects */ public class SimpleCreateArrayOfObjectsExample { public static void main(String[] args) { Integer[] myTestArray = new Integer[4]; System.out.println(myTestArray[0]); System.out.println(myTestArray[1]); System.out.println(myTestArray[2]); System.out.println(myTestArray[3]); } }We replaced int with Integer, which mean the array is now composed by a list of Objects. Now by default, an object has the value null when not initialized. Hence the output of the code above will be:
null null null nullIf we want the array to have values, then we could initialize each item. For example:
/** * A Simple Example that Creates an Array of Objects and Initialze each item. */ public class SimpleCreateArrayOfObjectsWithInitializationExample { public static void main(String[] args) { Integer[] myTestArray = new Integer[4]; myTestArray[0] = new Integer(5); myTestArray[1] = new Integer(7); myTestArray[2] = new Integer(9); myTestArray[3] = new Integer(11); System.out.println(myTestArray[0]); System.out.println(myTestArray[1]); System.out.println(myTestArray[2]); System.out.println(myTestArray[3]); } }
Will now have the output:
5 7 9 11
/** * A Simple Example that Declares And Initialise A Java Array Separately. */ public class InitializeJavaArray { public static void main(String[] args) { int[] myTestArray = {5, 7, 11}; System.out.println(myTestArray[0]); System.out.println(myTestArray[1]); System.out.println(myTestArray[2]); } }The curly brackets tells Java that we wish to create an array and the values are the ones contained within. In the example above, an array of 3 items is created with values 5, 7, and 11 respectively. hence the output of the code will be:
5 7 11For more related examples, kindly check this previous post about Initializing Arrays