Grails Cookbook - A collection of tutorials and examples

Java String Array Declaration

An array is a basic and essential data structure in Java. It holds a fix number of values that can be easily manipulated. It is common to use an array of immutable objects in Java, such as a String. But before we can use a String Array, we must first be able to declare it. Below are some examples on how to declare a String Array in Java.

Simple String Array Declaration in Java

Just like any other type of array, we can use the square brackets to declare an array of String. There are two ways of doing this. The first one is to use the square bracket after the variable name, for example:
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.

Declare String Array in Java with Size

An array is similar to a List in a way that both data structure can hold multiple values of some data type. But what differentiate an array over a List is that it has a fixed size. It means that an instance of a String Array can hold a fixed number of items, and that size can't be changed unless we recreate a new instance. Here is a simple example on how we can declare a Java String Array in Java with an initial size:
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: -1
Similarly, 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

Declare String Array in Java with Initial Values

It is more common that we wish to declare a String Array with initial values. For example, this is how we declare an array with initial contents that have names of fruits:
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:


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.