Java String Handling

Java String array example

Java String array example shows how to define and initialize Java String array. Example also shows how to add elements and access elements from String array using for loop.

How to define Java String array?

Java String array is a collection of fixed number of Java String objects. You can define String array in below given two methods.

This will define String array of size 3. That means we can add 3 String objects to an array using the index as given below.

If you have String variable, you can also directly assign it to the array using its index like given below.

Important Note: Java array starts from index 0. Since we defined our array’s size as 3, array will have three elements at 0, 1 and 2 index. Accessing index 3 will throw ArrayIndexOutOfBoundsException as given below.

If you know the String objects array is going to have at time of array definition, you can directly assign them while defining the String array as given below.

In this case, you do not have to define the size of an array. If you do so, there will be a compilation error.

For more information, please visit How to initialize String array example.

Java String array length

If you want to know the length of String array, you can use its length property.

This method returns Java String array length


Iterate Java String array

There are various ways using which you can iterate String array. You can use while loop, do while loop, for loop and enhanced for loop to iterate it.

Iterate String array using for loop

You can iterate String array using for loop with the help of array’s length as given below.


Note that we started with i as 0 and continued our loop till i is less than array length because array elements starts at 0 index and goes up to array size – 1 index.

Iterate String array using enhanced for loop

You can also use enhanced for loop to iterate the String array.


Here we do not have to specify the size of an array.

Some other examples of Java array

String array initialize example
String array remove duplicates example
Print array example
Sort String array example
Reverse array example
String array to List example

Refer to all array examples and string examples to learn more. Please let us know your views in the comments section below.

About the author



rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

Your email address will not be published. Required fields are marked *