In this post, we will see how to declare String array in java.
Table of Contents
Declare String array in java
There are 2 ways to declare String array in java.
Declaring a String array without size
1 2 3 |
String[] myStrArr; // Declaring a String array without size |
In this declaration, a String array is declared as any normal variable without size. Before using this array, you need to instantiate it otherwise you might get variable might not have initialized error.
Declaring a String array with size
1 2 3 |
String[] myStrArr = new String[3]; // Declaring a String array with size |
In this declaration, a String array is declared and instantiated at the same time. You can directly use this array, but you will see that myStrArr
contains null values as it is not initialized yet.
Let’s see both types with the help of example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String[] myStrArr1; //declared string array without size String[] myStrArr2 = new String[3]; //declared string array with size //System.out.println(myStrArr1[0]); //If you uncomment this line, then you will get // variable might not have been initialized System.out.print(Arrays.toString(myStrArr2)); } } |
Output:
How to declare and initialize String array in java
There are multiple ways to declare and initialize array in java.
Using new operator
We can declare and initialize array in java using new operator. We don’t have to provide size in this case.
1 2 3 |
String[] myStrArr = new String[]{"One","Two","Three"}; |
You can also use variable name before []
.
1 2 3 |
String myStrArr[] = new String[]{"One","Two","Three"}; |
Without Using new operator
We can also declare and initialize array in java without using new operator.
1 2 3 |
String[] myStrArr = {"One","Two","Three"}; |
Splitting declaration and initialization of String array
We can also declare a String array and can initialize it later.
1 2 3 4 |
String[] myStrArr1; myStrArr1 = new String[]{"One","Two","Three"}; |
Using Arrays.fill() to initialize String array
We can also declare a String array and can initialize it later.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; public class Main { public static void main(String args[]) { String[] myStrArr = new String[7]; // put value "One" to each array element Arrays.fill(myStrArr, "One"); // put value "Two" from index 3, inclusive, to index 5, exclusive Arrays.fill(myStrArr, 3, 5, "Two"); System.out.println(Arrays.toString(myStrArr)); } } |
Output:
Using Java 8’s setAll() method to initialize String array
We can also use Java 8’s setAll()
method and pass a generator to initialize String array in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; import java.util.Arrays; public class StringArrayMain { public static void main(String[] args) { String[] strArr = new String[7]; Arrays.setAll(strArr, str -> "One"); System.out.println(Arrays.toString(strArr)); } } |
Output:
How to declare and initialize an empty String array
There are two ways to create empty String array in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; public class Main { public static void main(String args[]) { //1st String[] myStrArr1 = new String[0]; System.out.println(Arrays.toString(myStrArr1)); // 2nd String[] myStrArr2 = {}; System.out.println(Arrays.toString(myStrArr2)); } } |
Output:
That’s all about how to declare a String array in java.