1. Define Array.
An array in Java is a collection of similar data types stored in contiguous memory locations.
It is used to store multiple values in a single variable, instead of declaring separate variables for each
value.
Important Properties of Arrays:
1. Fixed Size: Once an array is created, its size cannot be changed.
2. Indexed: Array elements are accessed using indices (starting from 0).
3. Homogeneous: All elements in an array must be of the same data type.
4. Stored in Contiguous Memory: Elements are stored one after another in memory.
5. Length Property: array.length gives the size of the array.
6. Random Access: You can directly access any element using its index
2. Two Different Ways to Define an Array:
Method 1: Declaration + Initialization Separately
int[] numbers; // Declare
numbers = new int[5]; // Allocate memory for 5 integers
Method 2: Declaration + Initialization Together
int[] numbers = {10, 20, 30, 40, 50};
3. Define 2D Array.
A 2D array is an array of arrays — also called a matrix or table, with rows and columns.
It is used to represent data in tabular form.
Declaration of a 2D Array:
int[][] matrix = new int[3][4]; // 3 rows and 4 columns
You can also define it with initialization:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};