☕ Java Beginner

What is an array in Java?

Answer

An array in Java is a fixed-size, ordered collection of elements of the same type. Once created, its size cannot change. Arrays are objects and are stored in the heap. Declare and create: int[] numbers = new int[5]; or int[] numbers = {1, 2, 3, 4, 5};. Access elements with zero-based indices: numbers[0]. Accessing an index out of bounds throws ArrayIndexOutOfBoundsException. Multi-dimensional arrays are arrays of arrays: int[][] matrix = new int[3][3];. The length property (not a method) gives the array size. For dynamic collections, use ArrayList instead.