In this post, we’ll illustrate how to declare and initialize an array of string in Java.

1. We can declare and initialize an array of string in Java by using a new operator with an array initializer. For example, the following code snippet creates an array of string of size 5:

 
2. Following is an alternate syntax for declaring an array similar to C/C++ style arrays, where [] appears after the variable name.

 
3. We can also split the code into declaration and assignment, as shown below

 
4. We can also declare and initialize an array of string in a single line without using a new operator, as shown below:

 
5. If we don’t provide any initializer, the default value of null will be assigned to each array element. For example,

 
6. We can use the Arrays.fill() method to assign a specified value to each element or elements between the specified range of the specified array. To illustrate, consider the following code:

 
7. We can also use the Arrays.setAll() method introduced with Java 8, which can be used to set all elements of the specified array using the specified generator function. For example,

 
8. We can also create an empty string array of size 0, as shown below:

or

 
9. We can also create an array of string using reflection. The following code will create a new array with the specified string type of length 5 with a default value of null.

 
10. We know that the array’s length is fixed, and we can modify it after the array is created. If we need a resizable-array implementation, we should go for an ArrayList that automatically grows or shrinks.

That’s all about initializing an array of Java String.