How to declare string array in Java
Introduction
Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be stored in the arrays.
To understand the string arrays, we need to know the basics of Java arrays and Java strings.
In this tutorial, we will learn about string array and how to declare a string array in Java.
What is string array ?
String array is an array containing fixed number of string values.
The string is sequence of characters It is called as immutable object, i.e., its values cannot be modified. As, string is considered an object in Java, string array is called array of objects.
Note: The main() method argument itself accepts string array as argument.
Similar to other arrays in Java, the first element of string array is placed at index 0, second at index 1, and so on.
Declaration of String Array
There are two ways to declare string array in Java:
- By specifying the size of array
- Without specifying the size of array
Syntax:
//declaring the string array without specifying the size
String[] array_name ;
//declaring the string array with size
String[] array_name = new String [10] ;
In the first type of declaration, we declare string array like a normal variable without mentioning its size. However, to use this array, we need to instantiate it with new keyword or initialize it with the string values. If we do not initialize it will give a compile time error as shown below.

In the second type of declaration, string array is declared and instantiated using new keyword. Here, the size of array is 10 i.e it can hold ten string values.
When we directly print the declared array, without initializing or adding elements in it, all the null values are printed
Example:
Let’s consider the following example where we declare and initialize the string array in Java.
DeclareString.java
public class DeclareString{
public static void main(String[] args) {
//declaring without specifying size
String[] strArray;
//declaring with size 3
String[] strArrayWithSize = new String[3];
//we cannot print strArray (first array) without initializing it
//System.out.println(strArray[0]);
//if we print strArrayWithsize (second array) without initializing
//it will print null
System.out.println(strArrayWithSize[0] + " " + strArrayWithSize[1] + " " + strArrayWithSize[2]);
}
}
Output:

In this way, we have learned how to declare a string array in Java.