The Nature of Arrays in Java:
An Array is an indexed collection of elements of the same data type (homogeneous) allocated in contiguous memory blocks on the Heap. In Java, arrays are true objects, instantiated using the new operator.
Three Stages of Array Creation:
- Declaration:
int[] arr;(Creates a reference variable on the Stack; currentlynull). - Instantiation:
arr = new int[5];(Allocates contiguous Heap memory for 5 integers initialized to0). - Initialization: Populating values individually (
arr[0] = 10;) or via inline array literal (int[] arr = {10, 20, 30, 40, 50};).
arr[-1] or arr[arr.length] causes an immediate runtime crash.