How to initialize a Java string array

String arrays in Java can be initialized in the following way:

  1. You can initialize using braces: You can directly initialize a string array using braces, listing the content of each string within the braces.
String[] array = {"Hello", "World"};
  1. Initialize using the new keyword: You can create a string array of a specific length using the new keyword, and assign values one by one.
String[] array = new String[2];
array[0] = "Hello";
array[1] = "World";

Note: When using the new keyword to initialize a string array, you need to first allocate memory space for the array and then assign a value to each element.

bannerAds