Introduction to Arrays
An array is a construct that provides for the storage of a list of items of the same type. Each item in an array is called an element, and each element is accessed by its numerical index.

In Java, arrays are first-class objects. Java arrays have their own behavior that is encapsulated into their definition when the compiler creates them. Since arrays are objects, array variables behave like class-type variables. It is important to distinguish between the array variable and the array instance to which it refers. Declaring an array only declares the array variable. To instantiate the array, the new operator must be used with the [] operator to enclose the array size. The array size can be any integer expression.
Array elements are always initialized according to their type as follows:
| Type | Initialization |
| integers and floating point | zero |
| characters | null |
| booleans | false |
| class-type objects | null |
Syntax
type variableName[];
type[] variableName;
type variableName[] = new type [length];
A Java array is the optional placement of the square brackets in the array declaration. You are allowed to place the square brackets either after the variable type or after the identifier.
The type of the array defines elements data type. The new operator is used to initialize the array. The array length means how many elements the array can hold.
Example
1. /**
2. * @(#)ArrayExample.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/10/28
7. */
8.
9. public class ArrayExample {
10.
11. /**
12. * Creates a new instance of ArrayExample.
13. */
14. public ArrayExample() {
15. }
16.
17. /**
18. * @param args the command line arguments
19. */
20. public static void main(String[] args) {
21. // TODO code application logic here
22.
23. int[] numbers; // declares an array of integers
24.
25. numbers = new int[10]; // allocates memory for 10 integers
26.
27. numbers[0] = 100; // initialize first element
28. numbers[1] = 200; // initialize second element
29. numbers[2] = 300; // etc.
30. numbers[3] = 400;
31. numbers[4] = 500;
32. numbers[5] = 600;
33. numbers[6] = 700;
34. numbers[7] = 800;
35. numbers[8] = 900;
36. numbers[9] = 1000;
37.
38. System.out.println("Element at index 0: " + numbers[0]);
39. System.out.println("Element at index 1: " + numbers[1]);
40. System.out.println("Element at index 2: " + numbers[2]);
41. System.out.println("Element at index 3: " + numbers[3]);
42. System.out.println("Element at index 4: " + numbers[4]);
43. System.out.println("Element at index 5: " + numbers[5]);
44. System.out.println("Element at index 6: " + numbers[6]);
45. System.out.println("Element at index 7: " + numbers[7]);
46. System.out.println("Element at index 8: " + numbers[8]);
47. System.out.println("Element at index 9: " + numbers[9]);
48.
49. }
50. }
Output
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
