String myArray[];The other way of declaring String Array in Java is to place the square brackets after the data type. For example:
String[] myArray;The two declaration above will have the same effect. But note that these are just declaration statements, the variables themselves have no initial values. They can not be used as is as modern IDE or compilers will complain if we use a variable that has not been initialized.
Note also that single pair of square brackets denotes a one dimension array.
String[] myArray= new String[10];This declares a variable myArray as a one dimensional String Array with size of 10. The variable myArray itself is not null, but it's individual contents are all null after this declaration. As discussed above, myArray has a fix length of 10 and that size can't change throughout the lifetime of the object. We can however change the size by creating a new instance on assigning it to the variable. For example:
String[] myArray= new String[10]; myArray= new String[20];The first line declares the variable myArray as a Java Array with size 10, and we can only change the size if we assign to it a new instance of an array. Note however that doing so will lose the original data. For example:
String[] myArray= new String[10]; myArray[0] = "Hello"; myArray= new String[20]; System.out.println(myArray[0] );This will output null to the screen as the original String Array is lost and replaced with a new array. If you are new to arrays, square brackets are also used to access each individual contents of an array. For example:
String[] myArray= new String[3]; myArray[0] = "Cat"; myArray[1] = "Dog"; myArray[2] = "Elephant";We can place the index of the item we wish to access inside the square brackets. Not that in Java, the index starts with zero, while some other programming language starts with one. The last index is the size of the array minus one. As shown above, if the size of the array is three, the last index is two. If we try to access an item with negative index, it will throw an exception. For example:
String[] myArray= new String[3]; myArray[-1] = "Cat";Will throw
Exception: class java.lang.ArrayIndexOutOfBoundsException: -1Similarly, if we go beyond the maximum index, the same exception will be thrown, for example:
String[] myArray= new String[3]; myArray[5] = "Cat";Will result to:
Exception: class java.lang.ArrayIndexOutOfBoundsException: 5
String[] fruitArray = {"Apple", "Banana", "Orange", "Grapes"};Notice that we do not need to specify the size of the array. The contents will deduce the required size. From the example above, the length of the String Array is four. We can modify the sample to check the size and contents:
String[] fruitArray = {"Apple", "Banana", "Orange", "Grapes"}; System.out.println( fruitArray.length ); System.out.println( fruitArray[0] ); System.out.println( fruitArray[1] ); System.out.println( fruitArray[2] ); System.out.println( fruitArray[3] );
The code will output:
4 Apple Banana Orange Grapes
Another way of declaring a String Array in Java with initial values is to use the new operator. Here is a simple example of this:
String[] fruitArray = new String[] {"Apple", "Banana", "Orange", "Grapes"};The result or effect is similar to the declaration earlier above.