How to create array of strings in Java?

Java 8Object Oriented ProgrammingProgramming

In Java, you can create an array just like an object using the new keyword. The syntax of creating an array in Java using new keyword −

type[] reference = new type[10];

Where,

  • type is the data type of the elements of the array.

  • reference is the reference that holds the array.

And, if you want to populate the array by assigning values to all the elements one by one using the index −

reference [0] = value1;
reference [1] = value2;

You can declare an array of Strings using the new keyword as &mius;

String[] str = new String[5];

And then, you can populate the string using the indices as −

str[0] = "JavaFX";
str[1] = "OpenCV";
str[2] = "ApacheFlume";
str[3] = "Apache Hadoop";
str[4] = "WebGL";

You can also create a String array directly using the curly braces as −

String str = str[0] = {"JavaFX", "OpenCV", "ApacheFlume", "Apache Hadoop", "WebGL"}
raja
Published on 09-Jan-2018 05:11:40
Advertisements