7/21/2017 - 3:37 AM

array declaration and initialization

array declaration and initialization

int[] myIntArray = new int[3];
int[] myIntArray = {1,2,3};
int[] myIntArray = new int[]{1,2,3};

For classes, for example String, it's the same:

String[] myStringArray = new String[3];
String[] myStringArray = {"a","b","c"};
String[] myStringArray = new String[]{"a","b","c"};
The third way of initialising is useful when you declare the array first and then initialise it. Cast is necessary here.

String[] myStringArray;
myStringArray = new String[]{"a","b","c"};