- array is a collection of similar type of elements which has contiguous memory location.
- arrays in Java are 0-indexed
int[] marks; // declaration
marks = new int[5]; // memory allocation
marks[0] = 51; // initialization
marks[1] = 33;
marks[2] = 44;
marks[3] = 67;
marks[4] = 98;
// traversing an array
for(int i=0; i<marks.length; i++){
System.out.print(marks[i] + " ");
}
-
- Both will take the same time because in both cases we know the exact memory location.
-
- Heap will only allocate new memory space only if you use the new keyword.
- Otherwise, a simple shallow copy will take place.
-
Functions:
- When we pass arrays as a parameter to a function only shallow copy takes place.
- And if any changes occur inside the function then the changes will be reflected everywhere.
- Random access: we can get any data located at an index position
- Size limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.