Array Memory Allocation
Array Memory Allocation
Key Points:
1. Declaration: When you declare an array, you only specify its type and name. No memory is
allocated at this stage.
2. int[] arr;
3. Instantiation: Memory is allocated when you use the new keyword to create the array and
specify its size.
5. Initialization: You can initialize the array elements either at the time of allocation or later.
7. Memory Allocation:
o Each element in the array is initialized to its default value (e.g., 0 for integers, null for
objects, false for booleans).
Example:
int[] numbers;
numbers[i] = i + 1;
System.out.println(num);
}
Notes:
If you need a resizable array, consider using classes like ArrayList from the java.util package.
Java performs automatic garbage collection, so you don't need to manually free the memory
allocated for arrays.
Diagram:
Diagram:2
Diagram:3