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++)
	                                    // we can create like this also
    String cities[] = {"Hyderabad", "Secunderabad", "Vizag"};
    for(int i = 0; i < cities.length; 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++)

String Array
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.

2 thoughts on “String Array Java”

  1. Irshad Kashmiri

    suppose i have String “abcdeacfedc” . i have to receive it through cammond line argument how can i find the frequecy of each element

Leave a Comment

Your email address will not be published.