String Array in Java

Last Updated : 17 Sept 2026

In Java, an array is a collection of similar data type elements. It uses a contiguous memory location to store the elements.

What is a String Array?

A string array is an array of a fixed number of String values. A String is a sequence of characters. Generally, a string is an immutable object, which means the value of the string cannot be changed. The string array works similarly to other data types.

Consider the following points about the String Array:

  • It is an object of the Array.
  • It can be declared by the two methods; by specifying the size or without specifying the size.
  • It can be initialized either at the time of declaration or by populating the values after the declaration.
  • The elements can be added to a string array after declaring it.
  • The string array can be iterated using the for loop.
  • The searching and sorting operation can be performed on the string array.

Declaration of String Array

We can declare string array in the following two ways:

Without specifying the size of the Array

By specifying the size of the Array

Another way of declaring the Array is String strArray[], but the above-specified methods are more efficient and recommended.

Initialization of String Array

We can initialize string array in the following ways:

Inline Initialization

Declaration with Initialization

Initialization after Declaration with Specific Size

In the above initialization, the value of the index can be found using the (arraylength - 1) formula. If we try to access the elements more than the index 2, it throws the Java.lang.ArrayIndexOutOfBoundsException exception.

Declaration with Initialization of an Empty Array

Iteration (Traversing) of String Array

A string array can be traversed using both the for loop and the enhanced for-each loop. These loops allow us to access and process each element of the array by iterating through the index of each element in the case of a for loop, or directly accessing each element in the case of a for-each loop.

Example

The following example demonstrates how to iterate over a string array containing names of students in a class using both a for loop and a for-each loop:

Java

Execute Now

Output:

Using for loop:
Ani
Sam
Joe

Using for-each loop:
Ani
Sam
Joe

Adding Elements to a String Array

We can easily add the elements to the String Array just like other data types. It can be done in the following three ways:

  • Using Pre-Allocation of the Array
  • Using the Array List
  • By Creating a New Array

1. Using Pre-Allocation of the Array

In this method, we already have an Array of larger size. For example, if we require to store the 10 elements, then we will create an Array of size 20. It is the easiest way to expand the Array elements.

Consider the following example to add elements in a pre-allocated array.

Java

Execute Now

Output:

Original Array Elements:[A, B, C, D, E, null, null]
Array after adding two elements:[A, B, C, D, E, F, G]

From the above example, we have added two elements in a pre-allocated Array.

2. Using ArrayList

The ArrayList is a fascinating data structure of the Java collection framework. We can easily add elements to a String Array using an ArrayList as an intermediate data structure.

Consider the following example to understand how to add elements to a String Array using ArrayList:

Java

Execute Now

Output:

Initial Array:
[A, B, C, D, E, F]
Array with added value: 
[A, B, C, D, E, F, G]

3. By Creating a New Array

In this method, we will create a new Array with a larger size than the initial Array and accommodate the elements in it. We will copy all the elements to the newly added Array. Consider the following example:

Java

Execute Now

Output:

Initial Array: [A, B, C]
updated Array: [A, B, C, D]

Searching in String Array

To search for a specific string in a string array, we use a for loop. It iterates over each element and compare each element with the target element. It both string gets matched, returns the position of the string.

Example: Searching in String Array

Java

Execute Now

Output:

Sam String is found at index 1

Explanation

In the above example, we have initialized a boolean variable x to false and an index variable to iterate through the string. Also, we have declared a local variable String variable s to be searched. Here, the break keyword will exit the loop as soon as the string is found.

Sorting in String Array

The Sort a String Array is quite easy. It is performed like sorting traditional array. We use a sort() method to sort the Array elements.

Java

Execute Now

Output:

Entered Sports: [Cricket, Basketball, Football, Badminton, Tennis]
Sorted Sports: [Badminton, Basketball, Cricket, Football, Tennis]

From the above example, we can see the elements from a String Array is sorted using the sort() method.

We can also convert String Array to other data structures such as List, int Array, ArrayList, and more and vice-versa.

Manipulating String Arrays

String arrays support various operations for manipulation, such as modifying elements. However, it's important to remember that Java arrays have a fixed size once initialized. Therefore, dynamic operations like adding or removing elements cannot be performed directly on arrays; for such operations, you may need to use other data structures like ArrayList.

Example: Modifying Elements in a String Array

The following example demonstrates how to modify an element in a string array:

Java

Execute Now

Output:

Ani
Sara
Joe

Common Use Cases

String arrays find application in numerous scenarios across Java programming. Some common use cases include:

  • Storing and Processing Data: String arrays are often used to store and process lists of strings, such as names, addresses, or any textual data.
  • Command-Line Arguments: In Java applications, string arrays are employed to handle command-line arguments passed to the main() method.
  • Data Structures: String arrays serve as building blocks for implementing more complex data structures like stacks, queues, and hash tables.