Initialize an array of String in Java
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:
|
1 |
String[] arr = new String[] { "A", "B", "C", "D", "E" }; |
2. Following is an alternate syntax for declaring an array similar to C/C++ style arrays, where [] appears after the variable name.
|
1 |
String[] arr = new String[] { "A", "B", "C", "D", "E" }; |
3. We can also split the code into declaration and assignment, as shown below
|
1 2 |
String[] arr; // array declaration arr = new String[] { "A", "B", "C", "D", "E" }; // array initialization |
4. We can also declare and initialize an array of string in a single line without using a new operator, as shown below:
|
1 |
String[] arr = { "A", "B", "C", "D", "E" }; |
5. If we don’t provide any initializer, the default value of null will be assigned to each array element. For example,
|
1 2 |
String[] arr = new String[5]; System.out.println(Arrays.toString(arr)); // [null, null, null, null, null] |
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:
|
1 2 3 4 5 6 7 8 9 |
String[] arr = new String[5]; // assign value `A` to each array element Arrays.fill(arr, "A"); // assign value `B` from index 1, inclusive, to index 3, exclusive Arrays.fill(arr, 1, 3, "B"); System.out.println(Arrays.toString(arr)); // [A, B, B, A, A] |
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,
|
1 2 3 |
String[] arr = new String[5]; Arrays.setAll(arr, i -> "A"); System.out.println(Arrays.toString(arr)); // [A, A, A, A, A] |
8. We can also create an empty string array of size 0, as shown below:
|
1 2 |
String[] arr = new String[0]; System.out.println(Arrays.toString(arr)); // prints [] |
or
|
1 2 |
String[] arr = {}; System.out.println(Arrays.toString(arr)); // prints [] |
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.
|
1 2 |
String[] arr = (String[]) Array.newInstance(String.class, 5); System.out.println(Arrays.toString(arr)); // [null, null, null, null, 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.
|
1 2 |
List<String> ArrayList = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E")); System.out.println(ArrayList); // [A, B, C, D, E] |
That’s all about initializing an array of Java String.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)