Udemy logo

string array javaA Java class library provides several classes to store collections of data that can be homogeneous (of same type) or heterogeneous (of different types). Heterogeneous collection of data is stored in the classes belonging to Java collection packaged whereas homogenous collections of data are stored in generic collections. Apart from classes, Java provides basic data types that can store a collection of homogeneous data. One such data type is the Java array data type. Arrays are used to store collections of data of the same type. The following example demonstrates syntax of a basic integer type array:

int num [] = new int [10];

To declare an array, you declare the type of elements that array will store, the name of the array and the opening and closing square brackets. This only declares an array. To initialize an array, the keyword “new” is used followed by the type of data the array will hold and the number of items in the square brackets. There are other ways to initialize arrays in Java, which is explained later in this article.

To learn more about String arrays in Java, look at this Udemy.com course

The String Array in Java

Strings are basically a sequence of characters that convey some meaningful interpretation to the user. In software applications, strings are often used to get input from the user such as user name, password, and date of birth. In Java, a string can be stored in the form of a collection in multiple ways such as they can be stored in the List collection, Hashtables, Hashmaps and Dictionary type classes. However, it is always better to use primitive data types over objects types. As previously stated, arrays can hold a collection of virtually any data type, therefore they can also be used to store a collection of strings. Arrays that hold string data types are called String arrays in Java.

Declaring and Initializing a String array

As previously mentioned, an array can be initialized in multiple ways. The following example demonstrates how a string type array can be declared and initialized using elements in individual indexes:

String countries [] = new String[5];
countries [0] = "USA";
countries [1] = "UK";
countries [2] = "CANADA";
countries [3] = "RUSSIA";
countries [4] = "CHINA";

In the above example, a String array named countries is declared and this array can hold 5 elements of the data type string. A string uses zero-based indexes, which means that the first element of a string would be stored at index zero.

If a string can store n elements, the first element would be stored at the 0th index and the last element would be stored at the n-1st index.

In the last example, the number of elements to be stored was specified during array initialization. If another element is added in the above countries array, an ArrayIndexOutOfBoundsException error is returned.

Another way to initialize array is directly inserting element during initialization. This eliminates the need for specifying the number of elements the array would hold. The following example demonstrates this concept:

String countries [] = {"USA", "UK","CHINA","FRANCE","RUSSIA"};

In the above code snippet, the string type values are directly added into the countries array without even using the new keyword and without specifying any index.

Interested in learning more about Java? See this Udemy.com course

Traversing a String Array in Java

Traversing a String type array is an extremely simple process. The most efficient process for traversing the elements of a String type array in java is using loops. For this purpose, a for-loop can be used that loops from 0 to the total length of array -1 index. The following example demonstrates this concept:

String countries [] = {"USA", "UK","CHINA","FRANCE","RUSSIA"};
for (int i=0; i < countries.length;i++)

In the above example, a for-loop is initialized with an iteration integer named “i” starting with the value 0. The most important line of the above example is the termination statement in the for-loop, which is the length of the array.

Calculating Length of the String

The method “length” can be called on an instance of the String type array, which returns the total length of the array. In the last example where the for-each loop is used to traverse all the elements, the termination statement states that the for-loop is executed until the value one less than length arrays is reached.

For instance, in the last example, the for loop starts from 0th index and executes till n-1 index which would be 4. Loop through the indexes 0 to 4, all the elements in the countries array are traversed.

A for-each Loop for Traversing String Array

The for-loop technique for traversing arrays looks a bit clumsy. In the termination condition, the length method has to be called on the instance of the array. There is a work around for this.  Since Java 5, there is now a Java for-each loop that iterates over collection types including arrays. The loop returns all the values in a collection in read-only format. The following example demonstrates its use:

String countries [] = {"USA", "UK","CHINA","FRANCE","RUSSIA"};
for (String s:countries)

In the above code a for-each loop has been implement to display all the country names in the countries, array. The syntax of a for-each loop is simple. Inside the loop parameters, a temporary variable with type name is declared followed by the collection to iterate upon, separated by a colon. Every time the loop executes, the temporary variables pointer is make to point at the next value in the array.

For instance, in the last example, a temporary variable s of the data type string is declared inside the array parameter body followed by a semicolon and then countries. This loop iterates through all the strings in the countries collection. Inside the body of the loop, these string values are printed on the console. Since the loop is read-only, the for-each loop returns read-only values and values cannot be deleted or changed.

For more detailed Java programming tutorials, take this Udemy.com course

Page Last Updated: May 2014

Top courses in Java

Java for Beginners
Navin Reddy
4.6 (1,304)
Java Interview Help
Bharath Thippireddy
4.5 (1,493)
Design Patterns in Java
Dmitri Nesteruk
4.3 (8,433)
Oracle Java Certification - Pass the Associate 1Z0-808 Exam.
Tim Buchalka's Learn Programming Academy, Goran Lochert
4.6 (5,756)
Java SE 11 Developer 1Z0-819 OCP Course - Part 1
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.6 (3,984)
Learn Selenium with Java, Cucumber & Frameworks
Pavan Kumar
4.6 (7,153)
Java 8 New Features In Simple Way
4.6 (15,115)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo