Way2Java

String Array Java

In Java, a String Array is nothing but an array of String objects. Elements are simply strings.

In Java, an array can be created with primitive data types and also objects. Objects can be of any type, even Employee objects. As String is a class and we can be an object of String, an array of strings can be created.

Following code explains String Array creation and retrieval of elements.
public class Demo
{
  public static void main(String args[])
  {
    String str1 = "S N Rao";
    String str2 = "Jyothi";
    String str3 = "Jyostna";

    String names[] = {str1, str2, str3};
    for(int i = 0; i < names.length; i++)
    {
      System.out.println(names[i]);
    }
	                                    // we can create like this also
    String cities[] = {"Hyderabad", "Secunderabad", "Vizag"};
    for(int i = 0; i < cities.length; i++)
    {
      System.out.println(cities[i]);
    }
	                                   // we can do like this also
    String fruits[] = new String[4];
    fruits[0] = "Mango";
    fruits[1] = "Banana";
    fruits[2] = "Pineapple";
    fruits[3] = "Grapes";
    for(int i = 0; i < fruits.length; i++)
    {
      System.out.println(fruits[i]);
    }
  }
}



Output screen on String Array Java

In the names array string objects are stored. In cities array, directly strings are included. Here string arrays are initialized. fruits array is created and values for elements are assigned later.

Know also how to create char array, int array and also concatenating arrays in Java style.