Java String Array: Understanding Different Aspects of String Arrays in Java

In programming, an array is a container that holds a limited number of objects, all of which must be of the same type. You can think of it as a medicine box, or a toolbox that holds only spanners. A string, on the other hand, is any sequence of characters. These can be words, random alphabets, and even numbers.

A string array, thus, is a ‘container’ that holds strings. We will learn more about string arrays in Java and examples of how to use them in this article. For a more in-depth exploration of string arrays, check out this introductory course on Java programming.

What is a String Array in Java?

A string array in Java is nothing more than a sequence of strings.  The total number of strings must be fixed, but each string can be of any length.  For example, a string array of length three with contents {“orange”, “pear”, “apple”} could be constructed in the following manner:

String arrays are relatively straightforward, and Java provides a moderate amount of built-in functionality to make them easy to work with.

Below, we will cover the two basic aspects of string arrays: initialization and iteration.  After that, we will walk through some examples that illustrate more advanced usage: sorting and searching, and copying ranges of elements.  Finally, we will supply a list of references that provide more in-depth information.

String Array Initialization

String arrays can be initialized in a number of different ways.

The most compact form (which was illustrated in the introduction) is inline initialization:

However, there are other ways to initialize a string array.  The string array variable and the contents of the string array can be completely decoupled:

The obvious question now is, “how are these two initialization methods different, and where should I use them?”

Inline initialization is well suited to scenarios where the number of strings is relatively small and you happen to know at compile-time which strings should be in the array. It’s a quick and easy way to get in, get out, but you need to know a lot of details about the strings to be included to use this method.

The second, more verbose initialization is well suited to scenarios where the number of strings is relatively large and it’s not necessarily known at compile-time what the strings will be.  For example, if you had to read a file of 100 employee names and then save each name in a string array (of length 100 in this case), you would use the verbose, decoupled initialization form.  In that case, the code might look a little more like the following:

Did the above sound all Greek to you? Try this beginner’s course on Java to clear away your doubts!

String Array Iteration

There are also a few ways to iterate over a string array.

Basic iteration is performed with classic iteration syntax found in many programming languages:

A more advanced type of iteration is illustrated below.  It has syntax that is very compact.  This type of iteration is sometimes called “foreach”.

Again, the question arises: which iteration method should you use, and why?

Basic iteration is well suited to scenarios where you may need to write to an element of the array in addition to possibly analyzing its existing contents.

The advanced iteration syntax is well suited to scenarios where you do not need to write to the array.  To illustrate this concept, let’s try to write to a string array using the advanced iteration syntax, and then inspect the array afterwards:

The result:

As you can see, the array elements themselves were not changed. This may not be intuitive at first, so let’s explain what’s going on. The compact syntax is simply shorthand for retrieving each element from the array. If you change the value of the loop variable (s in the example above), you are only changing a temporary variable. Although theoretically it could have been designed to work differently, this is how this syntax works. Ultimately, it is probably easiest to think of this advanced iteration syntax as readonly syntax.

To learn more about string iteration, check out this course on learning Java from scratch.

Advanced Usage: Sorting and Searching

Sorting and searching are the foundation of algorithms and data structures.  When it comes to arrays, the search algorithm of choice in many scenarios is binary search.  But to perform this search on an array, you must first make sure the array is sorted.

Java provides auxiliary array functionality inside a class called java.util.Array.  We will illustrate binary search with java.util.Arrays.sort() and java.util.Arrays.binarySearch():

The result:

Advanced Usage: Copying a Range of Elements

It is sometimes necessary to copy a range of elements from an array.  java.util.Arrays.copyOfRange() is a handy built-in method for doing this. You must specify the source array, the starting inclusive index, and the ending exclusive index. The only gotcha you must be aware of is the last parameter. It’s an exclusive delimiter, meaning you must specify the index of the element that is just after the final element you wish to copy (when in doubt, always check the Java documentation!).

The result:

Java string arrays are relatively easy to work with.  In this article, we covered the basics of string arrays: initialization and iteration.  In particular, we provided a rationale for choosing different syntax for both.  We also looked at more advanced topics: sorting and searching, and copying ranges of elements.  Armed with this information, you should be able to experiment with string arrays on your own as well as use them in real applications. If you want to learn more about java string arrays, check out this free course on Java design patterns and architecture.

Have any tips, tricks and advice on learning Java string arrays? Share them with us below!