How to create an array of strings in Java?

To create a string array, you can use the following syntax:

String[] arrayName = new String[length];

In this case, arrayName is the name of the array and length is the size of the array.

For example, to create an array containing 5 string elements, you can use the following code:

String[] array = new String[5];

To assign values to an array, you can access the elements of the array using their index and assign values to them. For example:

array[0] = "Hello";
array[1] = "World";
array[2] = "Java";
array[3] = "is";
array[4] = "awesome";

You can also initialize elements when creating an array. For example:

String[] array = {"Hello", "World", "Java", "is", "awesome"};

Note: Array indexes start at 0, so the index of the first element is 0, the second element is 1, and so on.

bannerAds