Java String array syntax and examples

When working with a Java String array -- or any other type of Java array -- you follow these steps to work with the array:

  1. You declare the array.
  2. You specify the size of the array.
  3. You populate the array.

You can create a String array in these three explicit steps, or you can do them in two steps, or just one step.

Three steps to using a Java String array

In the three-step approach to creating and using a Java String array, Step 1 is to declare the array:

public class Pizza {
  // declare a string array named 'toppings'
  private String[] toppings;
}

At this point the JVM knows that toppings is going to be an array of strings (the Java String type), but it doesn't know anything about the size of the array.

Next, in Step 2, somewhere later in your program you specify the array size:

public void sizeArray() {
  toppings = new String[3];
}

At this point I've declared that toppings is a Java String array that contains three elements. Now the array has a known size, but each element in the array is null because we haven't put any strings in those elements. (I like to think of array elements as "buckets", and right now there is nothing in any of those buckets, so by default, the are null.)

Now that our String array has a size, in Step 3 we can add elements to the array:

toppings[0] = "Cheese";
toppings[1] = "Sausage";
toppings[2] = "Pepperoni";

Note that like in the C programming language, and most other programming languages, the first element of a Java array begins with the number zero, then one, then two. This is referred to as being a "zero-based array".

Creating a Java String array in fewer steps

As mentioned, you don't have to follow these three steps in such an explicit manner. You can also create a String array with three elements (buckets) like this:

private String[] toppings = new String[3];

Or you can declare, size, and populate the array elements in one step, like this:

private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};

I describe all of this in much greater detail in my Java String array examples on the devdaily.com website.

Java String array syntax

As some final notes, you can place the square array brackets in several different places when you declare your arrays. Either of these two array syntax examples will work:

private String[] toppings;
private String toppings[];

The first approach is generally preferred, because it clearly shows that we're declaring an array of the String type.

You can also do crazy things with whitespace when declaring your arrays, but none of these approaches are recommended:

private String [] toppings;
private String toppings [];

private String [ ] toppings;
private String toppings [ ];

In summary, I hope these Java String array syntax examples have been helpful. For more information, here's a link to the Java String documentation, and as mentioned earlier, here's a link to my Java String tutorial on the devdaily.com website. Finally, here's a link to the Java programming language page on the Wikipedia site.

Valley Programming is currently a one-person business, owned and operated by Alvin Alexander. If you’re interested in anything you read here, feel free to contact me at “al” at (“@”) this website name (“valleyprogramming.com”), or at the phone number shown below. I’m just getting back to business here in November, 2021, and eventually I’ll get a contact form set up here, but until then, I hope that works.