Java String array examples (tutorial)
Java String/array FAQ: How do I create an array of Strings in Java (i.e., a Java String array)?
I wrote this article because I can never remember the Java array syntax myself, so I use this page as a reference. There are a couple of ways of declaring and using Java String arrays, and I'll try to cover those in this article.
1) Declaring, sizing, and using a Java String array
The first way to use a Java String array is to (a) declare it in one step, and then (b) use it in a second step. Within a class, you declare a Java String array like this:
private String[] fruits = new String[20];
You can then add elements to your new String array in a Java method like this:
fruits[0] = "apple"; fruits[1] = "banana"; fruits[3] = "orange";
2) Declare the String array size later
A second way to use a Java String array is to declare it in the first step, without declaring its size, like this:
private String[] fruits;
All you're really doing here is creating a reference named "fruits", and you'll need to specify the size of the String array later, like this:
fruits = new String[20];
Then you can add your fruits to the array as before:
fruits[0] = "apple"; fruits[1] = "banana"; fruits[3] = "orange";
3) Completely define your Java String array in one step
Another way to create and use a Java String array is to declare and populate the String array in one step. The syntax for doing this is like this:
// declare and populate a java string array String[] formData = { "apple" "banana" "orange" };
This covers the three main ways to declare and use String arrays in your Java applications.
Bonus: How to create a 2D Java String array
For a little extra credit today, here's another Java source code example that demonstrates how to create a two-dimensional Java String array (2d Java String array). I took this example from a Java/Swing prototype where I was populating a JTable:
// a 2d java string array Object[][] data = { {"CUSTOMER REP", "JOHN ELWAY", "1001" }, {"TECH SERVICE", "PEYTON MANNING", "1002" }, {"TECH SERVICE", "TOM BRADY", "1003" }, {"ACCOUNTING", "FRED FLINSTONE", "1004" } };
This example shows (a) how to create a Java 2D array, and also shows (b) that I can assign this array of Strings to something besides a Java String, specifically a reference that is of the Java Object type. In this simple example there's no need to do this, but if my 2D array contained different types of Java objects (String, Integer, etc.), this technique can be very handy.
Java String arrays - Summary
I hope these Java String array examples have been helpful. I've written more about Java arrays over on my devdaily.com website, including this Java String array tutorial which also demonstrates how to iterate over a String array using a Java 5 for loop.
Recent blog posts
- Free: Introduction To Functional Programming video training course
- The #1 functional programming (and computer programming) book
- The User Story Mapping Workshop process
- Alvin Alexander, Certified Scrum Product Owner (CSPO)
- Alvin Alexander is now a Certified ScrumMaster (CSM)
- Our “Back To Then” app (for iOS and Android)
- A Docker cheat sheet
- Pushing a Scala 3 JAR/Docker file to Google Cloud Run
- Reading a CSV File Into a Spark RDD (Scala Cookbook recipe)
- Reading a File Into a Spark RDD (Scala Cookbook recipe)