1int intArray[]; //declaring array
2intArray = new int[20]; // allocating memory to array
3//OR
4int[] intArray = new int[20]; // combining both statements in one
1int[] array1 = new int[5]; //int array length 5
2String[] array2 = new String[5] //String array length 5
3double[] array3 = new double[5] // Double array length 5
4
1An array is an ordered collection of elements of the same type, identified by a pair of square brackets [].
2
3 To use an array, you need to:
41. Declare the array with a name and a type. Use a plural name for array, e.g., marks, rows, numbers. All elements of the array belong to the same type.
52. Allocate the array using new operator, or through initialization, e.g.
6
7 int[] marks; // Declare an int array named marks
8 // marks contains a special value called null.
9int marks[]; // Same as above, but the above syntax recommended
10marks = new int[5]; // Allocate 5 elements via the "new" operator
11// Declare and allocate a 20-element array in one statement via "new" operator
12int[] factors = new int[20];
13// Declare, allocate a 6-element array thru initialization
14int[] numbers = {11, 22, 33, 44, 55, 66}; // size of array deduced from the number of items
1// both are valid declarations
2int intArray[];
3or int[] intArray;
4
5byte byteArray[];
6short shortsArray[];
7boolean booleanArray[];
8long longArray[];
9float floatArray[];
10double doubleArray[];
11char charArray[];
12
13// an array of references to objects of
14// the class MyClass (a class created by
15// user)
16MyClass myClassArray[];
17
18Object[] ao, // array of Object
19Collection[] ca; // array of Collection
20 // of unknown type